From 7f7145fb64d978da7df44c68eb70f7ea57ca7e9c Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Tue, 30 May 2023 11:18:15 -0400 Subject: [PATCH 1/5] Add Array.fill use case --- bench/array-append.js | 132 ++++++++++++++++++++++++++---------------- 1 file changed, 82 insertions(+), 50 deletions(-) diff --git a/bench/array-append.js b/bench/array-append.js index 2845fc33b..5eb1316a4 100644 --- a/bench/array-append.js +++ b/bench/array-append.js @@ -1,68 +1,100 @@ 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('fixed size 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('fixed size 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('variable size 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 + }) + + console.log() +} + +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) +} From 151d94e7a3a1a1974005cad04309efd0bac4e9e0 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Tue, 6 Jun 2023 16:02:30 -0400 Subject: [PATCH 2/5] Update bench/array-append.js Co-authored-by: Rafael Gonzaga --- bench/array-append.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/array-append.js b/bench/array-append.js index 5eb1316a4..b78710081 100644 --- a/bench/array-append.js +++ b/bench/array-append.js @@ -15,7 +15,7 @@ const bench = (name, total, fn) => { function compare (total) { console.log(tableHeader) - bench('fixed size fill(a,b,c) DESC', total, () => { + 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) }) From ffe02e9173071274a1806e64f92b437b5d5d50d4 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Tue, 6 Jun 2023 16:02:47 -0400 Subject: [PATCH 3/5] Remove console.log Co-authored-by: Rafael Gonzaga --- bench/array-append.js | 1 - 1 file changed, 1 deletion(-) diff --git a/bench/array-append.js b/bench/array-append.js index b78710081..99fc44a16 100644 --- a/bench/array-append.js +++ b/bench/array-append.js @@ -50,7 +50,6 @@ function compare (total) { for (let i = total; i-- > 0;) array[i] = i }) - console.log() } function compareStrings (total) { From f50d8f234e7b9a1d8050c5d4b0e5a2d1c0d544a1 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Tue, 6 Jun 2023 16:03:22 -0400 Subject: [PATCH 4/5] update label bench/array-append.js Co-authored-by: Rafael Gonzaga --- bench/array-append.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/array-append.js b/bench/array-append.js index 99fc44a16..007cb601b 100644 --- a/bench/array-append.js +++ b/bench/array-append.js @@ -25,7 +25,7 @@ function compare (total) { for (let i = 0; i < total; i++) array.push(i) }) - bench('fixed size array[i] = i', total, () => { + bench('new Array(length) + array[i] = i', total, () => { const array = new Array(total) for (let i = 0; i < total; i++) array[i] = i }) From 309738d5d78a986e8088c90cf674391a377dee39 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Tue, 6 Jun 2023 16:03:31 -0400 Subject: [PATCH 5/5] update label bench/array-append.js Co-authored-by: Rafael Gonzaga --- bench/array-append.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/array-append.js b/bench/array-append.js index 007cb601b..d336b0a5c 100644 --- a/bench/array-append.js +++ b/bench/array-append.js @@ -35,7 +35,7 @@ function compare (total) { for (let i = 0; i < total; i++) array.fill(i, i, i+1) }) - bench('variable size array[i] = i', total, () => { + bench('array[i] = i', total, () => { const array = [] for (let i = 0; i < total; i++) array[i] = i })