diff --git a/bench/array-append.js b/bench/array-append.js index 2845fc33b..d336b0a5c 100644 --- a/bench/array-append.js +++ b/bench/array-append.js @@ -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) + }) + + bench('array[i] = i', total, () => { + const array = [] + for (let i = 0; i < total; i++) array[i] = i + }) + + bench('fixedArray[i] = i DESC', total, () => { + 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') + }) + + 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) +}