-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: improve performance of validateStringArray and validateBooleanArray
PR-URL: #49756 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
- Loading branch information
Showing
8 changed files
with
390 additions
and
5 deletions.
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,61 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e7], | ||
value: [ | ||
"'777'", | ||
'0o777', | ||
], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
function getParseFactory() { | ||
const { | ||
parseFileMode, | ||
} = require('internal/validators'); | ||
|
||
return (n) => parseFileMode(n, 'n'); | ||
} | ||
|
||
function main({ n, value }) { | ||
const parse = getParseFactory(); | ||
|
||
value = value === "'777'" ? '777' : 0o777; | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
let errCase = false; | ||
|
||
for (let i = 0; i < length; ++i) { | ||
try { | ||
array.push(parse(value)); | ||
} catch (e) { | ||
errCase = true; | ||
array.push(e); | ||
} | ||
} | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
try { | ||
array[index] = parse(value); | ||
} catch (e) { | ||
array[index] = e; | ||
} | ||
} | ||
|
||
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], errCase ? 'object' : 'number'); | ||
} | ||
} |
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,68 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e7], | ||
value: [ | ||
'[]', | ||
'[1,2,3]', | ||
], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
function getValidateFactory() { | ||
const { | ||
validateArray, | ||
} = require('internal/validators'); | ||
|
||
return (n) => validateArray(n, 'n'); | ||
} | ||
|
||
function main({ n, value }) { | ||
const validate = getValidateFactory(); | ||
|
||
switch (value) { | ||
case '[]': | ||
value = []; | ||
break; | ||
case '[1,2,3]': | ||
value = [1, 2, 3]; | ||
break; | ||
} | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
let errCase = false; | ||
|
||
for (let i = 0; i < length; ++i) { | ||
try { | ||
array.push(validate(value)); | ||
} catch (e) { | ||
errCase = true; | ||
array.push(e); | ||
} | ||
} | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
try { | ||
array[index] = validate(value); | ||
} catch (e) { | ||
array[index] = e; | ||
} | ||
} | ||
|
||
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], errCase ? 'object' : 'undefined'); | ||
} | ||
} |
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,55 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e8], | ||
code: [ | ||
'validateBoolean', | ||
], | ||
value: [ | ||
'true', | ||
'false', | ||
], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
function getValidateFactory(code) { | ||
const { | ||
validateBoolean, | ||
} = require('internal/validators'); | ||
|
||
switch (code) { | ||
case 'validateBoolean': | ||
return (n) => validateBoolean(n, 'n'); | ||
} | ||
} | ||
|
||
function main({ n, code, value }) { | ||
const validate = getValidateFactory(code); | ||
const v = value === 'true'; | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
for (let i = 0; i < length; ++i) { | ||
array.push(validate(v)); | ||
} | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
array[index] = validate(v); | ||
} | ||
|
||
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], 'undefined'); | ||
} | ||
} |
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,59 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e8], | ||
encoding: [ | ||
'ascii', | ||
'utf8', | ||
'utf-8', | ||
'utf16le', | ||
'ucs2', | ||
'ucs-2', | ||
'base64', | ||
'latin1', | ||
'binary', | ||
'hex', | ||
], | ||
value: [ | ||
'test', | ||
], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
function getValidateFactory(encoding) { | ||
const { | ||
validateEncoding, | ||
} = require('internal/validators'); | ||
|
||
return (n) => validateEncoding(n, encoding); | ||
} | ||
|
||
function main({ n, encoding, value }) { | ||
const validate = getValidateFactory(encoding); | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
for (let i = 0; i < length; ++i) { | ||
array.push(validate(value)); | ||
} | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
array[index] = validate(value); | ||
} | ||
|
||
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], 'undefined'); | ||
} | ||
} |
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,69 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e7], | ||
code: [ | ||
'validateOneOf', | ||
], | ||
value: [ | ||
'fifo', | ||
'lifo', | ||
'lilo', | ||
], | ||
validLength: [ | ||
1, | ||
2, | ||
3, | ||
], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
const validValues = [ | ||
'fifo', | ||
'lifo', | ||
'lilo', | ||
'filo', | ||
]; | ||
|
||
function getValidateFactory(code, validLength) { | ||
const { | ||
validateOneOf, | ||
} = require('internal/validators'); | ||
|
||
switch (code) { | ||
case 'validateOneOf': | ||
return (n) => validateOneOf(n, 'n', validValues.slice(0, validLength)); | ||
} | ||
} | ||
|
||
function main({ n, code, validLength }) { | ||
const validate = getValidateFactory(code, validLength); | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
|
||
const value = validValues[validLength - 1]; | ||
|
||
for (let i = 0; i < length; ++i) { | ||
array.push(validate(value)); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
array[index] = validate(value); | ||
} | ||
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], 'undefined'); | ||
} | ||
} |
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: [1e8], | ||
type: [ | ||
'validateStringArray', | ||
'validateBooleanArray', | ||
], | ||
arrayLength: [ | ||
0, | ||
1, | ||
10, | ||
100, | ||
], | ||
}, { | ||
flags: ['--expose-internals'], | ||
}); | ||
|
||
function getValidateFactory(type, arrayLength) { | ||
const { | ||
validateBooleanArray, | ||
validateStringArray, | ||
} = require('internal/validators'); | ||
|
||
switch (type) { | ||
case 'validateBooleanArray': | ||
return [ | ||
(n) => validateBooleanArray(n, 'n'), | ||
Array.from({ length: arrayLength }, (v, i) => ((i & 1) === 0)), | ||
]; | ||
case 'validateStringArray': | ||
return [ | ||
(n) => validateStringArray(n, 'n'), | ||
Array.from({ length: arrayLength }, (v, i) => `foo${i}`), | ||
]; | ||
} | ||
} | ||
|
||
function main({ n, type, arrayLength }) { | ||
const [validate, value] = getValidateFactory(type, arrayLength); | ||
|
||
// Warm up. | ||
const length = 1024; | ||
const array = []; | ||
for (let i = 0; i < length; ++i) { | ||
array.push(validate(value)); | ||
} | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; ++i) { | ||
const index = i % length; | ||
array[index] = validate(value); | ||
} | ||
|
||
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], 'undefined'); | ||
} | ||
} |
Oops, something went wrong.