GeezerVison is showing github.com — lib: improve performance of validateStringArray and validateBooleanArray · nodejs/node@251ae1d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +61Lines changed: 61 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +const bench = common.createBenchmark(main, { | | 7 | + n: [1e7], | | 8 | + value: [ | | 9 | + "'777'", | | 10 | + '0o777', | | 11 | + ], | | 12 | +}, { | | 13 | + flags: ['--expose-internals'], | | 14 | +}); | | 15 | + | | 16 | +function getParseFactory() { | | 17 | + const { | | 18 | + parseFileMode, | | 19 | + } = require('internal/validators'); | | 20 | + | | 21 | + return (n) => parseFileMode(n, 'n'); | | 22 | +} | | 23 | + | | 24 | +function main({ n, value }) { | | 25 | + const parse = getParseFactory(); | | 26 | + | | 27 | + value = value === "'777'" ? '777' : 0o777; | | 28 | + | | 29 | + // Warm up. | | 30 | + const length = 1024; | | 31 | + const array = []; | | 32 | + let errCase = false; | | 33 | + | | 34 | + for (let i = 0; i < length; ++i) { | | 35 | + try { | | 36 | + array.push(parse(value)); | | 37 | + } catch (e) { | | 38 | + errCase = true; | | 39 | + array.push(e); | | 40 | + } | | 41 | + } | | 42 | + | | 43 | + bench.start(); | | 44 | + | | 45 | + for (let i = 0; i < n; ++i) { | | 46 | + const index = i % length; | | 47 | + try { | | 48 | + array[index] = parse(value); | | 49 | + } catch (e) { | | 50 | + array[index] = e; | | 51 | + } | | 52 | + } | | 53 | + | | 54 | + bench.end(n); | | 55 | + | | 56 | + // Verify the entries to prevent dead code elimination from making | | 57 | + // the benchmark invalid. | | 58 | + for (let i = 0; i < length; ++i) { | | 59 | + assert.strictEqual(typeof array[i], errCase ? 'object' : 'number'); | | 60 | + } | | 61 | +} |
Collapse file Copy file name to clipboard +68Lines changed: 68 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +const bench = common.createBenchmark(main, { | | 7 | + n: [1e7], | | 8 | + value: [ | | 9 | + '[]', | | 10 | + '[1,2,3]', | | 11 | + ], | | 12 | +}, { | | 13 | + flags: ['--expose-internals'], | | 14 | +}); | | 15 | + | | 16 | +function getValidateFactory() { | | 17 | + const { | | 18 | + validateArray, | | 19 | + } = require('internal/validators'); | | 20 | + | | 21 | + return (n) => validateArray(n, 'n'); | | 22 | +} | | 23 | + | | 24 | +function main({ n, value }) { | | 25 | + const validate = getValidateFactory(); | | 26 | + | | 27 | + switch (value) { | | 28 | + case '[]': | | 29 | + value = []; | | 30 | + break; | | 31 | + case '[1,2,3]': | | 32 | + value = [1, 2, 3]; | | 33 | + break; | | 34 | + } | | 35 | + | | 36 | + // Warm up. | | 37 | + const length = 1024; | | 38 | + const array = []; | | 39 | + let errCase = false; | | 40 | + | | 41 | + for (let i = 0; i < length; ++i) { | | 42 | + try { | | 43 | + array.push(validate(value)); | | 44 | + } catch (e) { | | 45 | + errCase = true; | | 46 | + array.push(e); | | 47 | + } | | 48 | + } | | 49 | + | | 50 | + bench.start(); | | 51 | + | | 52 | + for (let i = 0; i < n; ++i) { | | 53 | + const index = i % length; | | 54 | + try { | | 55 | + array[index] = validate(value); | | 56 | + } catch (e) { | | 57 | + array[index] = e; | | 58 | + } | | 59 | + } | | 60 | + | | 61 | + bench.end(n); | | 62 | + | | 63 | + // Verify the entries to prevent dead code elimination from making | | 64 | + // the benchmark invalid. | | 65 | + for (let i = 0; i < length; ++i) { | | 66 | + assert.strictEqual(typeof array[i], errCase ? 'object' : 'undefined'); | | 67 | + } | | 68 | +} |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +const bench = common.createBenchmark(main, { | | 7 | + n: [1e8], | | 8 | + code: [ | | 9 | + 'validateBoolean', | | 10 | + ], | | 11 | + value: [ | | 12 | + 'true', | | 13 | + 'false', | | 14 | + ], | | 15 | +}, { | | 16 | + flags: ['--expose-internals'], | | 17 | +}); | | 18 | + | | 19 | +function getValidateFactory(code) { | | 20 | + const { | | 21 | + validateBoolean, | | 22 | + } = require('internal/validators'); | | 23 | + | | 24 | + switch (code) { | | 25 | + case 'validateBoolean': | | 26 | + return (n) => validateBoolean(n, 'n'); | | 27 | + } | | 28 | +} | | 29 | + | | 30 | +function main({ n, code, value }) { | | 31 | + const validate = getValidateFactory(code); | | 32 | + const v = value === 'true'; | | 33 | + | | 34 | + // Warm up. | | 35 | + const length = 1024; | | 36 | + const array = []; | | 37 | + for (let i = 0; i < length; ++i) { | | 38 | + array.push(validate(v)); | | 39 | + } | | 40 | + | | 41 | + bench.start(); | | 42 | + | | 43 | + for (let i = 0; i < n; ++i) { | | 44 | + const index = i % length; | | 45 | + array[index] = validate(v); | | 46 | + } | | 47 | + | | 48 | + bench.end(n); | | 49 | + | | 50 | + // Verify the entries to prevent dead code elimination from making | | 51 | + // the benchmark invalid. | | 52 | + for (let i = 0; i < length; ++i) { | | 53 | + assert.strictEqual(typeof array[i], 'undefined'); | | 54 | + } | | 55 | +} |
Collapse file Copy file name to clipboard +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +const bench = common.createBenchmark(main, { | | 7 | + n: [1e8], | | 8 | + encoding: [ | | 9 | + 'ascii', | | 10 | + 'utf8', | | 11 | + 'utf-8', | | 12 | + 'utf16le', | | 13 | + 'ucs2', | | 14 | + 'ucs-2', | | 15 | + 'base64', | | 16 | + 'latin1', | | 17 | + 'binary', | | 18 | + 'hex', | | 19 | + ], | | 20 | + value: [ | | 21 | + 'test', | | 22 | + ], | | 23 | +}, { | | 24 | + flags: ['--expose-internals'], | | 25 | +}); | | 26 | + | | 27 | +function getValidateFactory(encoding) { | | 28 | + const { | | 29 | + validateEncoding, | | 30 | + } = require('internal/validators'); | | 31 | + | | 32 | + return (n) => validateEncoding(n, encoding); | | 33 | +} | | 34 | + | | 35 | +function main({ n, encoding, value }) { | | 36 | + const validate = getValidateFactory(encoding); | | 37 | + | | 38 | + // Warm up. | | 39 | + const length = 1024; | | 40 | + const array = []; | | 41 | + for (let i = 0; i < length; ++i) { | | 42 | + array.push(validate(value)); | | 43 | + } | | 44 | + | | 45 | + bench.start(); | | 46 | + | | 47 | + for (let i = 0; i < n; ++i) { | | 48 | + const index = i % length; | | 49 | + array[index] = validate(value); | | 50 | + } | | 51 | + | | 52 | + bench.end(n); | | 53 | + | | 54 | + // Verify the entries to prevent dead code elimination from making | | 55 | + // the benchmark invalid. | | 56 | + for (let i = 0; i < length; ++i) { | | 57 | + assert.strictEqual(typeof array[i], 'undefined'); | | 58 | + } | | 59 | +} |
Collapse file Copy file name to clipboard +69Lines changed: 69 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +const bench = common.createBenchmark(main, { | | 7 | + n: [1e7], | | 8 | + code: [ | | 9 | + 'validateOneOf', | | 10 | + ], | | 11 | + value: [ | | 12 | + 'fifo', | | 13 | + 'lifo', | | 14 | + 'lilo', | | 15 | + ], | | 16 | + validLength: [ | | 17 | + 1, | | 18 | + 2, | | 19 | + 3, | | 20 | + ], | | 21 | +}, { | | 22 | + flags: ['--expose-internals'], | | 23 | +}); | | 24 | + | | 25 | +const validValues = [ | | 26 | + 'fifo', | | 27 | + 'lifo', | | 28 | + 'lilo', | | 29 | + 'filo', | | 30 | +]; | | 31 | + | | 32 | +function getValidateFactory(code, validLength) { | | 33 | + const { | | 34 | + validateOneOf, | | 35 | + } = require('internal/validators'); | | 36 | + | | 37 | + switch (code) { | | 38 | + case 'validateOneOf': | | 39 | + return (n) => validateOneOf(n, 'n', validValues.slice(0, validLength)); | | 40 | + } | | 41 | +} | | 42 | + | | 43 | +function main({ n, code, validLength }) { | | 44 | + const validate = getValidateFactory(code, validLength); | | 45 | + | | 46 | + // Warm up. | | 47 | + const length = 1024; | | 48 | + const array = []; | | 49 | + | | 50 | + const value = validValues[validLength - 1]; | | 51 | + | | 52 | + for (let i = 0; i < length; ++i) { | | 53 | + array.push(validate(value)); | | 54 | + } | | 55 | + | | 56 | + bench.start(); | | 57 | + for (let i = 0; i < n; ++i) { | | 58 | + const index = i % length; | | 59 | + array[index] = validate(value); | | 60 | + } | | 61 | + bench.end(n); | | 62 | + | | 63 | + | | 64 | + // Verify the entries to prevent dead code elimination from making | | 65 | + // the benchmark invalid. | | 66 | + for (let i = 0; i < length; ++i) { | | 67 | + assert.strictEqual(typeof array[i], 'undefined'); | | 68 | + } | | 69 | +} |
Collapse file Copy file name to clipboard +66Lines changed: 66 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +const bench = common.createBenchmark(main, { | | 7 | + n: [1e8], | | 8 | + type: [ | | 9 | + 'validateStringArray', | | 10 | + 'validateBooleanArray', | | 11 | + ], | | 12 | + arrayLength: [ | | 13 | + 0, | | 14 | + 1, | | 15 | + 10, | | 16 | + 100, | | 17 | + ], | | 18 | +}, { | | 19 | + flags: ['--expose-internals'], | | 20 | +}); | | 21 | + | | 22 | +function getValidateFactory(type, arrayLength) { | | 23 | + const { | | 24 | + validateBooleanArray, | | 25 | + validateStringArray, | | 26 | + } = require('internal/validators'); | | 27 | + | | 28 | + switch (type) { | | 29 | + case 'validateBooleanArray': | | 30 | + return [ | | 31 | + (n) => validateBooleanArray(n, 'n'), | | 32 | + Array.from({ length: arrayLength }, (v, i) => ((i & 1) === 0)), | | 33 | + ]; | | 34 | + case 'validateStringArray': | | 35 | + return [ | | 36 | + (n) => validateStringArray(n, 'n'), | | 37 | + Array.from({ length: arrayLength }, (v, i) => `foo${i}`), | | 38 | + ]; | | 39 | + } | | 40 | +} | | 41 | + | | 42 | +function main({ n, type, arrayLength }) { | | 43 | + const [validate, value] = getValidateFactory(type, arrayLength); | | 44 | + | | 45 | + // Warm up. | | 46 | + const length = 1024; | | 47 | + const array = []; | | 48 | + for (let i = 0; i < length; ++i) { | | 49 | + array.push(validate(value)); | | 50 | + } | | 51 | + | | 52 | + bench.start(); | | 53 | + | | 54 | + for (let i = 0; i < n; ++i) { | | 55 | + const index = i % length; | | 56 | + array[index] = validate(value); | | 57 | + } | | 58 | + | | 59 | + bench.end(n); | | 60 | + | | 61 | + // Verify the entries to prevent dead code elimination from making | | 62 | + // the benchmark invalid. | | 63 | + for (let i = 0; i < length; ++i) { | | 64 | + assert.strictEqual(typeof array[i], 'undefined'); | | 65 | + } | | 66 | +} |
|