-
Notifications
You must be signed in to change notification settings - Fork 21
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
Open
danielsan
wants to merge
6
commits into
RafaelGSS:main
Choose a base branch
from
danielsan:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f7145f
Add Array.fill use case
DanielSanEM 1ffc9d9
Merge branch 'main' of github.com:RafaelGSS/nodejs-bench-operations i…
DanielSanEM 151d94e
Update bench/array-append.js
danielsan ffe02e9
Remove console.log
danielsan f50d8f2
update label bench/array-append.js
danielsan 309738d
update label bench/array-append.js
danielsan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
bench('array[i] = i', total, () => { | ||
const array = [] | ||
for (let i = 0; i < total; i++) array[i] = i | ||
}) | ||
|
||
bench('fixedArray[i] = i DESC', total, () => { | ||
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 | ||
}) | ||
|
||
} | ||
|
||
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') | ||
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?