Rename Number.prototype.clz to Math.clz

Jason Orendorff jason.orendorff at gmail.com
Thu Jan 16 14:27:14 PST 2014


On Thu, Jan 16, 2014 at 3:12 PM, Jens Nockert <jens at nockert.se> wrote:
> On 2014/01/16, at 17:40, Jason Orendorff <jason.orendorff at gmail.com> wrote:
>> Or maybe: flip the function around so that it returns the number of
>> bits in the binary expansion of the value: Math.bitlen(15) === 4. This
>> is just (32 - CLZ), so it effectively computes the same thing as clz.
>> The advantage is that it extends naturally to integers of any size.
>
> What is Math.bitlen(-1) then? Isn’t this just the same problem as before, except it happens for negative numbers instead of positive?

No opinion.

For the use cases I know of, speed matters: it is desired that this
operation compile down to a CLZ or equivalent machine instruction,
when the argument is in the range of uint32. I slightly prefer
Math.clz32 to Math.bitlen for this reason. It's exactly what it says
on the label. People searching for a JS equivalent of CLZ or
__builtin_clz() are likely to find it. The specification can start
with ToUint32 which already tells us how corner cases should behave.

-j


More information about the es-discuss mailing list