class RandomObj {
constructor() {
this.valA = ~~(Math.random() * 255 + 0.5);
this.valB = ~~(Math.random() * 300 + 0.5);
}
}
const array1 = new Array(100000);
for (var i = 0; i < 100000; i ++) {
array1[i] = new RandomObj();
}
function performanceTest() {
let startTime = new Date();
for (var run = 0; run < 1000; run++) {
let count = 0;
for (var i = 0; i < 100000; i++) {
if (array1[i].valA > array1[i].valB) {
count += 1;
}
}
}
console.log(new Date() - startTime + 'ms');
}
performanceTest();
Copy and paste this code to developer tools or a new .html file.
On my machine (Windows 7 x64, Chrome 63) it takes around 1200-1600ms to execute.
However, when I uncomment the code snippet inside the loop, the execution time decreases to only 500-700ms.
I'm puzzled as to why this change in code affects the performance so significantly...