-
Notifications
You must be signed in to change notification settings - Fork 22
Add Array.fill to array append benchmarks #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
7f7145f
1ffc9d9
151d94e
ffe02e9
f50d8f2
309738d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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, () => { | ||
danielsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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('variable size array[i] = i', total, () => { | ||
danielsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const array = [] | ||
| for (let i = 0; i < total; i++) array[i] = i | ||
| }) | ||
|
|
||
| bench('fixedArray[i] = i DESC', total, () => { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| }) | ||
|
|
||
| console.log() | ||
danielsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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') | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.