Skip to content

Instantly share code, notes, and snippets.

@JaoodxD
Last active October 20, 2023 20:13
Show Gist options
  • Save JaoodxD/c562972eb0022fc3c8d205c1d51dd0c1 to your computer and use it in GitHub Desktop.
Save JaoodxD/c562972eb0022fc3c8d205c1d51dd0c1 to your computer and use it in GitHub Desktop.
Perf diff
'use strict'
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') // ~1050ms
}
console.log(res)
'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++) {
var tempSum = 0
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') // ~800ms
}
console.log(res)
'use strict'
var arr = new Array(100).fill(0).map((e, i) => i)
var res = 0
function myFor (arr) {
var temp = 0
for (let j = 0; j < 10_000_000; j++) {
var tempSum = 0
for (let index = 0; index < arr.length; index++) {
tempSum += arr[index]
}
temp += tempSum
}
res += temp
}
for (let c = 0; c < 5; c++) {
console.time('for')
myFor(arr)
console.timeEnd('for') // ~700ms
}
console.log(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment