Created
October 12, 2021 03:10
-
-
Save likev/2b303c9f5290fdb361f468af91dc9883 to your computer and use it in GitHub Desktop.
when use global variables,the for loop run 100x slow in firefox dev console!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
//when use global variables,the for loop run 100x slow! | |
const CHECK_LENGTH = 1E6, | |
CHECK_POINT = Math.ceil(Math.sqrt(CHECK_LENGTH)); | |
var test1 = _ => { | |
let cn = new Array(CHECK_LENGTH); | |
for (let j = 2; j < 1E3; j++) { | |
for (let i = j * j; i < 1E6; i++) { | |
} | |
} | |
} | |
var test2 = _ => { | |
let cn = new Array(CHECK_LENGTH); | |
for (let j = 2; j < CHECK_POINT; j++) { | |
for (let i = j * j; i < CHECK_LENGTH; i++) { | |
} | |
} | |
} | |
var t1 = performance.now() | |
test1() | |
console.log(`without global variables: took ${performance.now() - t1} milliseconds.`) | |
var t2 = performance.now() | |
test2() | |
console.log(`with global variables: took ${performance.now() - t2} milliseconds.`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment