-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
benchmark: add some assert benchmarks #54734
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
528a6a0
benchmark: add strictEqual and notStrictEqual bench
RafaelGSS c814568
benchmark: add throws and doesNotThrow bench
RafaelGSS e8cbad5
benchmark: add rejects and doesNotReject bench
RafaelGSS a2f1a3a
benchmark: add match and doesNotMatch bench
RafaelGSS 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 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [25, 2e7], | ||
method: ['match', 'doesNotMatch'], | ||
}); | ||
|
||
function main({ n, method }) { | ||
const fn = assert[method]; | ||
const actual = 'Example of string that will match'; | ||
const expected = method === 'match' ? /will match/ : /will not match/; | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; ++i) { | ||
fn(actual, expected); | ||
} | ||
bench.end(n); | ||
} |
This file contains 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||
'use strict'; | ||||||
|
||||||
const common = require('../common.js'); | ||||||
const assert = require('assert'); | ||||||
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.
Suggested change
|
||||||
|
||||||
const bench = common.createBenchmark(main, { | ||||||
n: [25, 2e5], | ||||||
method: ['rejects', 'doesNotReject'], | ||||||
}); | ||||||
|
||||||
async function main({ n, method }) { | ||||||
const fn = assert[method]; | ||||||
const shouldReject = method === 'rejects'; | ||||||
|
||||||
bench.start(); | ||||||
for (let i = 0; i < n; ++i) { | ||||||
await fn(async () => { | ||||||
const err = new Error(`assert.${method}`); | ||||||
if (shouldReject) { | ||||||
throw err; | ||||||
} else { | ||||||
return err; | ||||||
} | ||||||
}); | ||||||
} | ||||||
bench.end(n); | ||||||
} |
This file contains 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||
'use strict'; | ||||||
|
||||||
const common = require('../common.js'); | ||||||
const assert = require('assert'); | ||||||
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.
Suggested change
|
||||||
|
||||||
const bench = common.createBenchmark(main, { | ||||||
n: [25, 2e5], | ||||||
type: ['string', 'object', 'number'], | ||||||
method: ['strictEqual', 'notStrictEqual'], | ||||||
}); | ||||||
|
||||||
function main({ type, n, method }) { | ||||||
const fn = assert[method]; | ||||||
let actual, expected; | ||||||
switch (type) { | ||||||
case 'string': | ||||||
actual = expected = 'Hello World'; | ||||||
if (method === 'notStrictEqual') { | ||||||
expected += 'bar'; | ||||||
} | ||||||
break; | ||||||
case 'object': | ||||||
actual = expected = { a: 'Hello', b: 'World' }; | ||||||
if (method === 'notStrictEqual') { | ||||||
expected = { a: 'Hello', b: 'World' }; | ||||||
} | ||||||
break; | ||||||
case 'number': | ||||||
actual = expected = 1e9; | ||||||
if (method === 'notStrictEqual') { | ||||||
expected += 1; | ||||||
} | ||||||
break; | ||||||
default: | ||||||
throw new Error('Unexpected type'); | ||||||
} | ||||||
|
||||||
bench.start(); | ||||||
for (let i = 0; i < n; ++i) { | ||||||
fn(actual, expected); | ||||||
} | ||||||
bench.end(n); | ||||||
} |
This file contains 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||
'use strict'; | ||||||
|
||||||
const common = require('../common.js'); | ||||||
const assert = require('assert'); | ||||||
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.
Suggested change
|
||||||
|
||||||
const bench = common.createBenchmark(main, { | ||||||
n: [25, 2e5], | ||||||
method: ['throws', 'doesNotThrow'], | ||||||
}); | ||||||
|
||||||
function main({ n, method }) { | ||||||
const fn = assert[method]; | ||||||
const shouldThrow = method === 'throws'; | ||||||
|
||||||
bench.start(); | ||||||
for (let i = 0; i < n; ++i) { | ||||||
fn(() => { | ||||||
const err = new Error(`assert.${method}`); | ||||||
if (shouldThrow) { | ||||||
throw err; | ||||||
} else { | ||||||
return err; | ||||||
} | ||||||
}); | ||||||
} | ||||||
bench.end(n); | ||||||
} |
Oops, something went wrong.
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.