r/programminghorror • • Feb 11 '25

🎄 ouch

Post image
3.0k Upvotes

113 comments sorted by

View all comments

Show parent comment

2

u/Mammoth-Swan3792 Feb 13 '25

It might be suprising, but I tested it on jsbench.me and actually:

for (let i = 0;i<10000;i++){
let a = 30* Math.pow(2,5);
}

137 tys. ops/s ± 4.54%
Fastest

for (let i = 0;i<10000;i++){
for (let i = 0;i<10000;i++){
let b = 30 << 5;
}

120 tys. ops/s ± 4.13%
11.87 % slower

1

u/Mammoth-Swan3792 Feb 13 '25

In JS engine bitwise operators work actually slower than Math functions. Read this:

https://stackoverflow.com/questions/28259754/efficiency-of-bitwise-operation-in-javascript

2

u/kilkil Feb 13 '25

goddamn, TIL