UInt8ClampedArray Bitwise operators?

Daniel Ehrenberg dehrenberg at chromium.org
Wed Aug 12 00:26:13 UTC 2015

I don't think my earlier mail went through, so I'm posing it below for
the record:


SIMD types have bitwise operators, but SIMD.js exposes just fixed-size
vectors that are optimized to what hardware can optimize certain
operations for. A future extension (the "long SIMD API") may operate
on whole arrays. I wouldn't recommend using SIMD.js unless you really
feel like you're taking advantage of the better performance, and the
particular vector size works for your requirements.

The point of SIMD is to expose higher-performance hardware features to
users. You may want to use this for implementing bitwise operations in
user code. However, if you don't need that, it may be enough for you
to use existing operators & | ^ ~ etc, in a loop. A search on npm
yields tons of bitarray libraries which probably do this already,
though I haven't assessed how good they are.

If you were getting at operator overloading in particular, operators
are already well-defined on things like Uint8Array: Roughly speaking,
they will call .valueOf() and, if that results in a Number they will
do the operation on the underlying Number. There's no built-in valueOf
method for that object, but you can always monkeypatch one in. Here's
an example session in the V8 command-line shell:

d8> Uint8ClampedArray.prototype.valueOf = function() { return 1 }
function () { return 1 }
d8> new Uint8ClampedArray([1, 2]) << 3

The downside of doing anything beyond existing npm packages and
changing the language is that it increases complexity. What is the
upside you have in mind to building it into the language?

Have fun!

On Tue, Aug 11, 2015 at 5:24 PM, Brendan Eich <brendan at mozilla.org> wrote:
> Daniel Ehrenberg wrote:
>> Uint8ClampedArray is more of a historical artifact than something you
>> should actually aim to use. Uint8Array is probably what you'd want to
>> get at as a basis for a user-level library.
> Thanks for posting this, Daniel. Just to be super-clear (and I was around
> when it was born), Uint8ClampedArray is *totally* a historical artifact (of
> the HTML5 canvas element). Avoid unless you really are doing canvas-y
> things.
> /be

More information about the es-discuss mailing list