pierrec / js-cuint

C-like unsigned integers for Javascript
MIT License
32 stars 9 forks source link

Difference between toString and toNumber #4

Open MikeKovarik opened 8 years ago

MikeKovarik commented 8 years ago

Hello. I've been using this library through js-xxhash and i was wondering why does XXH('abcd', 0).toString(16) not equals XXH('abcd', 0).toNumber().toString(16)? Is there some standard it complies to? Other than that toNumber is much more performant and serves the same purpose.

var hash1 = XXH('abcd', 0);
var i, t1, t2, t3, t4;

t1 = performance.now();
for (i = 0; i < 100000; i++) {
    hash1.toString(16)
}
t2 = performance.now();

t3 = performance.now();
for (i = 0; i < 100000; i++) {
    Math.abs(hash1.toNumber()).toString(16);
}
t4 = performance.now();

console.log(t2 - t1);
console.log(t4 - t3);

results of this code in Chrome 49

563.4300000000001
53.80499999999995

It's even worse in Edge

13569.654713166914
96.97500422863777
MikeKovarik commented 8 years ago

So I looked into it and came up with a soluition #5

pierrec commented 8 years ago

Very good thank you. Some tests do fail though I will double check why when I have some time.