-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
errors: improve performance of instantiation
PR-URL: #49654 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
- Loading branch information
Showing
17 changed files
with
305 additions
and
137 deletions.
There are no files selected for viewing
File renamed without changes.
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,66 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
code: [ | ||
'built-in', | ||
'ERR_HTTP2_STREAM_SELF_DEPENDENCY', | ||
'ERR_INVALID_STATE', | ||
'ERR_INVALID_URL', | ||
], | ||
stackTraceLimit: [0, 10], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
function getErrorFactory(code) { | ||
const { | ||
ERR_HTTP2_STREAM_SELF_DEPENDENCY, | ||
ERR_INVALID_STATE, | ||
ERR_INVALID_URL, | ||
} = require('internal/errors').codes; | ||
|
||
switch (code) { | ||
case 'built-in': | ||
return (n) => new Error(); | ||
case 'ERR_HTTP2_STREAM_SELF_DEPENDENCY': | ||
return (n) => new ERR_HTTP2_STREAM_SELF_DEPENDENCY(); | ||
case 'ERR_INVALID_STATE': | ||
return (n) => new ERR_INVALID_STATE(n + ''); | ||
case 'ERR_INVALID_URL': | ||
return (n) => new ERR_INVALID_URL({ input: n + '' }); | ||
default: | ||
throw new Error(`${code} not supported`); | ||
} | ||
} | ||
|
||
function main({ n, code, stackTraceLimit }) { | ||
const getError = getErrorFactory(code); | ||
|
||
Error.stackTraceLimit = stackTraceLimit; | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
for (let i = 0; i < length; ++i) { | ||
array.push(getError(i)); | ||
} | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
array[index] = getError(index); | ||
} | ||
|
||
bench.end(n); | ||
|
||
// Verify the entries to prevent dead code elimination from making | ||
// the benchmark invalid. | ||
for (let i = 0; i < length; ++i) { | ||
assert.strictEqual(typeof array[i], 'object'); | ||
} | ||
} |
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,62 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
code: [ | ||
'built-in', | ||
'ERR_HTTP2_STREAM_SELF_DEPENDENCY', | ||
'ERR_INVALID_STATE', | ||
], | ||
stackTraceLimit: [0, 10], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
function getErrorStackFactory(code) { | ||
const { | ||
ERR_INVALID_STATE, | ||
ERR_HTTP2_STREAM_SELF_DEPENDENCY, | ||
} = require('internal/errors').codes; | ||
|
||
switch (code) { | ||
case 'built-in': | ||
return (n) => new Error().stack; | ||
case 'ERR_HTTP2_STREAM_SELF_DEPENDENCY': | ||
return (n) => new ERR_HTTP2_STREAM_SELF_DEPENDENCY().stack; | ||
case 'ERR_INVALID_STATE': | ||
return (n) => new ERR_INVALID_STATE(n + '').stack; | ||
default: | ||
throw new Error(`${code} not supported`); | ||
} | ||
} | ||
|
||
function main({ n, code, stackTraceLimit }) { | ||
const getStack = getErrorStackFactory(code); | ||
|
||
Error.stackTraceLimit = stackTraceLimit; | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
for (let i = 0; i < length; ++i) { | ||
array.push(getStack(i)); | ||
} | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
array[index] = getStack(index); | ||
} | ||
|
||
bench.end(n); | ||
|
||
// Verify the entries to prevent dead code elimination from making | ||
// the benchmark invalid. | ||
for (let i = 0; i < length; ++i) { | ||
assert.strictEqual(typeof array[i], 'string'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
Oops, something went wrong.