Skip to content

Instantly share code, notes, and snippets.

@cjdell
Created July 16, 2015 11:03
Show Gist options
  • Save cjdell/3822fd330859fcc5aa0b to your computer and use it in GitHub Desktop.
Save cjdell/3822fd330859fcc5aa0b to your computer and use it in GitHub Desktop.
Strange indexOf issue with large arrays. Works in some runtimes and not others (node)
// Run this script with Node.js to see it throw. Works fine in browsers for some reason. Maybe an optimisation bug
function loop() {
var arr = [0];
var i = 0;
//eval('(1)'); // ==== UNCOMMENTING THIS MAKES IT WORK
while (i < 4101) { // ==== MAKING THIS SMALLER MAKES IT WORK
var num = (i % 2) / 2; // ==== SIMPLIFYING THIS MAKES IT WORK
//console.log(num);
if (arr.indexOf(num) !== Array.prototype.indexOf.call(arr, num)) {
console.log('i', i);
console.log('num', num);
throw new Error('WTF this should not happen!');
}
i++;
}
}
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment