Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
fast function to reverse unsigned integer
// A fast function to reverse an integer (123 => 321) function reverse(num) { let i = 0, result = 0, max = 1; while(num > 1) { const exp = 10 ** i; const r = num / exp; const digit = r | 0; if (r >= 1 && r < 10) { if (i > max) max = i; result += digit * (10 ** (max - i)); num -= exp * digit; i = ~-i; } else { i = -~i; } } return result; } const test = 145775612, count = 10000; // Print test console.log(`${test} => ${reverse(test)}`); // Make reverse a hot function to get v8 to optimize this for (let i = 0; i < count / 10; ++i) reverse(test); // "Benchmarks" console.time('reverse'); for (let i = 0; i < count; ++i) reverse(test); console.timeEnd('reverse'); console.time('string'); for (let i = 0; i < count; ++i) test.toString().split('').reverse().join(''); console.timeEnd('string');
run
|
edit
|
history
|
help
1
ddd
JSON parse
Range List for JavaScript
Union of two sorted arrays
Count digits
apply
Funny stuff
To compare; creating async methods in javascript
neural spam network
Accept matching keys in object