mirror of https://github.com/lukechilds/node.git
Browse Source
internal/util.js definied toInteger() and toLength() but they were only used by buffer.js. Inlining these small functions results in a small but statistically-significant performance gain. PR-URL: https://github.com/nodejs/node/pull/12153 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>v6
Rich Trott
8 years ago
committed by
James M Snell
4 changed files with 8 additions and 87 deletions
@ -1,32 +0,0 @@ |
|||
// Flags: --expose-internals
|
|||
'use strict'; |
|||
|
|||
require('../common'); |
|||
const assert = require('assert'); |
|||
const {toInteger} = require('internal/util'); |
|||
|
|||
const expectZero = [ |
|||
'0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false, |
|||
'', ' ', undefined, null |
|||
]; |
|||
expectZero.forEach(function(value) { |
|||
assert.strictEqual(toInteger(value), 0); |
|||
}); |
|||
|
|||
assert.strictEqual(toInteger(Infinity), Infinity); |
|||
assert.strictEqual(toInteger(-Infinity), -Infinity); |
|||
|
|||
const expectSame = [ |
|||
'0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true |
|||
]; |
|||
expectSame.forEach(function(value) { |
|||
assert.strictEqual(toInteger(value), +value, `${value} is not an Integer`); |
|||
}); |
|||
|
|||
const expectIntegers = new Map([ |
|||
[[1], 1], [[-1], -1], [['1'], 1], [['-1'], -1], |
|||
[3.14, 3], [-3.14, -3], ['3.14', 3], ['-3.14', -3], |
|||
]); |
|||
expectIntegers.forEach(function(expected, value) { |
|||
assert.strictEqual(toInteger(value), expected); |
|||
}); |
@ -1,35 +0,0 @@ |
|||
// Flags: --expose-internals
|
|||
'use strict'; |
|||
|
|||
require('../common'); |
|||
const assert = require('assert'); |
|||
const {toLength} = require('internal/util'); |
|||
const maxValue = Number.MAX_SAFE_INTEGER; |
|||
|
|||
const expectZero = [ |
|||
'0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false, |
|||
'', ' ', undefined, null, -1, -1.25, -1.1, -1.9, -Infinity |
|||
]; |
|||
expectZero.forEach(function(value) { |
|||
assert.strictEqual(toLength(value), 0); |
|||
}); |
|||
|
|||
assert.strictEqual(toLength(maxValue - 1), maxValue - 1); |
|||
assert.strictEqual(maxValue, maxValue); |
|||
assert.strictEqual(toLength(Infinity), maxValue); |
|||
assert.strictEqual(toLength(maxValue + 1), maxValue); |
|||
|
|||
|
|||
[ |
|||
'0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true |
|||
].forEach(function(value) { |
|||
assert.strictEqual(toLength(value), +value > 0 ? +value : 0); |
|||
}); |
|||
|
|||
const expectIntegers = new Map([ |
|||
[[1], 1], [[-1], 0], [['1'], 1], [['-1'], 0], |
|||
[3.14, 3], [-3.14, 0], ['3.14', 3], ['-3.14', 0], |
|||
]); |
|||
expectIntegers.forEach(function(expected, value) { |
|||
assert.strictEqual(toLength(value), expected); |
|||
}); |
Loading…
Reference in new issue