Why does this log output show the same answer in each iteration?
Posted
by
Will Hancock
on Stack Overflow
See other posts from Stack Overflow
or by Will Hancock
Published on 2012-10-12T09:24:51Z
Indexed on
2012/10/12
9:37 UTC
Read the original article
Hit count: 213
JavaScript
OK, I was reading an article on optimising JS for Googles V8 engine, when i saw this code example...
I nearly skimmed over it, but then I saw this; |=; a[0] |= b;
a = new Array();
a[0] = 0;
for (var b = 0; b < 10; b++) {
console.log(a, b)
a[0] |= b; // Much better! 2x faster.
}
a[0] |= b;
So I ran it, in my console, with a console.log in the loop and resulted in 15;
[15] 0
[15] 1
[15] 2
[15] 3
[15] 4
[15] 5
[15] 6
[15] 7
[15] 8
[15] 9
WHAT?!?! Where the hell does it get 15 from, on every iteration?!?!?!
I've been a web dev for 7 years, and this has stumped me and a fellow colleague.
Can somebody talk me through this code?
Cheers.
© Stack Overflow or respective owner