-
-
Save JaoodxD/031f35c24ea9872f9ea029a7e33f12d9 to your computer and use it in GitHub Desktop.
15x perf difference
This file contains 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
var arr = new Array(100).fill(0).map((e, i) => i); | |
var res = 0 | |
function myFor(arr) { | |
for (let j = 0; j < 10_000_000; j++) { | |
for (let index = 0; index < arr.length; index++) { | |
res += arr[index]; | |
} | |
} | |
} | |
for (let c = 0; c < 5; c++) { | |
console.time('for') | |
myFor(arr) | |
console.timeEnd('for') // ~12600ms | |
} | |
console.log(res); |
This file contains 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
var arr = new Array(100).fill(0).map((e, i) => i); | |
var res = 0 | |
function myFor(arr) { | |
var tempSum = 0 | |
for (let j = 0; j < 10_000_000; j++) { | |
for (let index = 0; index < arr.length; index++) { | |
tempSum += arr[index]; | |
} | |
} | |
res += tempSum; | |
} | |
for (let c = 0; c < 5; c++) { | |
console.time('for') | |
myFor(arr) | |
console.timeEnd('for') // ~750ms | |
} | |
console.log(res); |
This file contains 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'; | |
var arr = new Array(100).fill(0).map((e, i) => i); | |
var res = 0 | |
function myFor(arr) { | |
for (let j = 0; j < 10_000_000; j++) { | |
res += arr.reduce((acc, n) => acc + n, 0); | |
// for (let index = 0; index < arr.length; index++) { | |
// res += arr[index]; | |
// } | |
} | |
} | |
for (let c = 0; c < 5; c++) { | |
console.time('for') | |
myFor(arr) | |
console.timeEnd('for') // ~840ms | |
} | |
console.log(res); |
This file contains 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' | |
var arr = new Array(100).fill(0).map((e, i) => i) | |
var res = 0 | |
function myFor (arr) { | |
let sum = 0 | |
for (let j = 0; j < 10_000_000; j++) { | |
sum += arr.reduce((acc, n) => acc + n, 0) | |
} | |
res += sum | |
} | |
for (let c = 0; c < 5; c++) { | |
console.time('for') | |
myFor(arr) | |
console.timeEnd('for') // ~730ms | |
} | |
console.log(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment