Skip to content

Instantly share code, notes, and snippets.

@koh110
Last active Oct 25, 2020
Embed
What would you like to do?

Array performance https://v8.dev/blog/elements-kinds#avoid-creating-holes

$ node -v
v14.14.0

$ node simple_loop.js 
loop: 475.427ms

$ node null_check.js 
loop: 474.855ms

$ node for_of.js
loop: 498.195ms

$ node for_each.js 
loop: 474.051ms

$ node array_fill.js     
array fill: 0.12ms

$ node array_fill_loop.js
array fill: 0.55ms

$ node array_fill_loop2.js
array fill: 0.36ms

$ node array_fill_map.js 
array fill: 0.152ms

$ node max_check.js 
999999
max: 12.62ms

$ node max_check2.js
999999
max: 12.719ms
// const TotalLanes = 31;
const TotalLanes = 10000;
const createLaneMap = (initial) => {
return new Array(TotalLanes).fill(initial)
}
console.time('array fill')
const arr = createLaneMap({ text: 'init' })
console.timeEnd('array fill')
// const TotalLanes = 31;
const TotalLanes = 10000;
const createLaneMap = (initial) => {
const laneMap = [];
for (let i = 0; i < TotalLanes; i++) {
laneMap.push(initial);
}
return laneMap;
}
console.time('array fill')
const arr = createLaneMap({ text: 'init' })
console.timeEnd('array fill')
// const TotalLanes = 31;
const TotalLanes = 10000;
const createLaneMap = (initial) => {
const laneMap = new Array(TotalLanes);
for (let i = 0; i < TotalLanes; i++) {
laneMap[i] = initial;
}
return laneMap;
}
console.time('array fill')
const arr = createLaneMap({ text: 'init' })
console.timeEnd('array fill')
// const TotalLanes = 31;
const TotalLanes = 10000;
const createLaneMap = (initial) => {
return new Array(TotalLanes).map(() => initial)
}
console.time('array fill')
const arr = createLaneMap({ text: 'init' })
console.timeEnd('array fill')
const items = []
const length = 1000000
for (let i = 0; i < length; i++) {
items.push({ index: i, val: i })
}
const doSomething = (item) => {
item.val = Math.pow(item.val, 3)
}
console.time('loop')
items.forEach((item) => {
doSomething(item)
})
console.timeEnd('loop')
const items = []
const length = 1000000
for (let i = 0; i < length; i++) {
items.push({ index: i, val: i })
}
const doSomething = (item) => {
item.val = Math.pow(item.val, 3)
}
console.time('loop')
for (const item of items) {
doSomething(item)
}
console.timeEnd('loop')
const items = []
const length = 1000000
for (let i = 0; i < length; i++) {
items.push(i)
}
function Maximum(array) {
let max = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] > max) max = array[i];
}
return max;
}
console.time('max')
console.log(Maximum(items))
console.timeEnd('max')
const items = []
const length = 1000000
for (let i = 0; i < length; i++) {
items.push(i)
}
function Maximum(array) {
let max = 0;
for (let i = 0; i <= array.length; i++) { // BAD COMPARISON!
if (array[i] > max) max = array[i];
}
return max;
}
console.time('max')
console.log(Maximum(items))
console.timeEnd('max')
const items = []
const length = 1000000
for (let i = 0; i < length; i++) {
items.push({ index: i, val: i })
}
const doSomething = (item) => {
item.val = Math.pow(item.val, 3)
}
console.time('loop')
for (let i = 0, item; (item = items[i]) != null; i++) {
doSomething(item)
}
console.timeEnd('loop')
const items = []
const length = 1000000
for (let i = 0; i < length; i++) {
items.push({ index: i, val: i })
}
const doSomething = (item) => {
item.val = Math.pow(item.val, 3)
}
console.time('loop')
for (let i = 0; i < items.length; i++) {
doSomething(items[i])
}
console.timeEnd('loop')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment