Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 81 additions & 50 deletions bench/array-append.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,99 @@
const { H2, createTableHeader } = require('../markdown')

function compareToMdTable(name, amount, ms) {
function compareToMdTable (name, amount, ms) {
const numberFormat = new Intl.NumberFormat()
return `${name}|${numberFormat.format(amount)}|${numberFormat.format(ms)}ms`
}

function compare(total) {
const arrayPush = performance.now();
var array = [];
for (var i = 0; i < total; i++) {
array.push(i);
}
const arrayPushTotal = performance.now() - arrayPush;

const preAlloc = performance.now();
var array2 = new Array(total);
for (var i = 0; i < total; i++) {
array2[i] = i;
}
const preAllocToal = performance.now() - preAlloc;

console.log(compareToMdTable('array.push', total, arrayPushTotal))
console.log(compareToMdTable('new Array(length)', total, preAllocToal))
const bench = (name, total, fn) => {
const start = performance.now()
fn()
const diff = performance.now() - start
console.log(compareToMdTable(name, total, diff))
}

function compareStrings(total) {
const arrayPush = performance.now();
var array = [];
for (var i = 0; i < total; i++) {
array.push('test');
}
const arrayPushTotal = performance.now() - arrayPush;

const preAlloc = performance.now();
var array2 = new Array(total);
for (var i = 0; i < total; i++) {
array2[i] = 'test'
}
const preAllocToal = performance.now() - preAlloc;

console.log(compareToMdTable('array.push', total, arrayPushTotal))
console.log(compareToMdTable('new Array(length)', total, preAllocToal))
function compare (total) {
console.log(tableHeader)

bench('new Array(length) + fill(a,b,c) DESC', total, () => {
const array = new Array(total)
for (let i = total; i-- > 0;) array.fill(i, i, i+1)
})

bench('array.push', total, () => {
const array = []
for (let i = 0; i < total; i++) array.push(i)
})

bench('new Array(length) + array[i] = i', total, () => {
const array = new Array(total)
for (let i = 0; i < total; i++) array[i] = i
})

bench('fixed size fill(a,b,c)', total, () => {
const array = new Array(total)
for (let i = 0; i < total; i++) array.fill(i, i, i+1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we expect this bench to be different from the first one?

})

bench('array[i] = i', total, () => {
const array = []
for (let i = 0; i < total; i++) array[i] = i
})

bench('fixedArray[i] = i DESC', total, () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question. Is it expected to be slow than ASC?

const array = new Array(total)
for (let i = total; i-- > 0;) array[i] = i
})

bench('variableArray[i] = i DESC', total, () => {
const array = []
for (let i = total; i-- > 0;) array[i] = i
})

}

function compareStrings (total) {
console.log(tableHeader)

bench('Array.fill(a,b,c)', total, () => {
const array = Array.from(total)
for (let i = 0; i < total; i++) array.fill('test', i, i + 1)
})

bench('array.push', total, () => {
const array = []
for (let i = 0; i < total; i++) array.push('test')
})

bench('array[i] = string', total, () => {
const array = new Array(total)
for (let i = 0; i < total; i++) array[i] = 'test'
})

bench('Array.from', total, () => {
const array = Array.from({ length: total }, () => 'test')
})

bench('Array.fill', total, () => {
const array = Array.from(total).fill('test')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This concat two operations. I don't think we should include it

})

console.log()
}

console.log(H2('Array.append (number)'))
const tableHeader = createTableHeader([
'type',
'amount',
'time elapsed',
'time elapsed'
])

console.log(tableHeader)
compare(10);
compare(100);
compare(1000);
compare(10000);
compare(1000000);
compare(100000000);
for (let i = 6; i <= 8; i++) {
compare(10 ** i)
}

console.log(H2('Array.append (string)'))
console.log(tableHeader)
compareStrings(10);
compareStrings(100);
compareStrings(1000);
compareStrings(10000);
compareStrings(1000000);
compareStrings(100000000);
// console.log(tableHeader)
for (let i = 6; i <= 8; i++) {
compareStrings(10 ** i)
}