From 5236d8cb765ffc775083099594e2217a401cd692 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 31 Mar 2017 22:48:35 -0700 Subject: [PATCH] buffer,util: refactor for performance 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. --- lib/buffer.js | 10 ++++-- lib/internal/util.js | 18 ---------- test/parallel/test-internal-util-toInteger.js | 32 ----------------- test/parallel/test-internal-util-toLength.js | 35 ------------------- 4 files changed, 8 insertions(+), 87 deletions(-) delete mode 100644 test/parallel/test-internal-util-toInteger.js delete mode 100644 test/parallel/test-internal-util-toLength.js diff --git a/lib/buffer.js b/lib/buffer.js index 1c686f36c61ea0..a70b0adab2e7b2 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -254,7 +254,9 @@ function fromArrayLike(obj) { } function fromArrayBuffer(obj, byteOffset, length) { - byteOffset = internalUtil.toInteger(byteOffset); + // convert byteOffset to integer + byteOffset = +byteOffset; + byteOffset = byteOffset ? Math.trunc(byteOffset) : 0; const maxLength = obj.byteLength - byteOffset; @@ -264,7 +266,11 @@ function fromArrayBuffer(obj, byteOffset, length) { if (length === undefined) { length = maxLength; } else { - length = internalUtil.toLength(length); + // convert length to non-negative integer + length = +length; + length = length ? Math.trunc(length) : 0; + length = length <= 0 ? 0 : Math.min(length, Number.MAX_SAFE_INTEGER); + if (length > maxLength) throw new RangeError("'length' is out of bounds"); } diff --git a/lib/internal/util.js b/lib/internal/util.js index 3de57040f90381..742abc617d60c0 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -142,24 +142,6 @@ exports.cachedResult = function cachedResult(fn) { }; }; -/* - * Implementation of ToInteger as per ECMAScript Specification - * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger - */ -const toInteger = exports.toInteger = function toInteger(argument) { - const number = +argument; - return Number.isNaN(number) ? 0 : Math.trunc(number); -}; - -/* - * Implementation of ToLength as per ECMAScript Specification - * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tolength - */ -exports.toLength = function toLength(argument) { - const len = toInteger(argument); - return len <= 0 ? 0 : Math.min(len, Number.MAX_SAFE_INTEGER); -}; - // Useful for Wrapping an ES6 Class with a constructor Function that // does not require the new keyword. For instance: // class A { constructor(x) {this.x = x;}} diff --git a/test/parallel/test-internal-util-toInteger.js b/test/parallel/test-internal-util-toInteger.js deleted file mode 100644 index 57a411964da90f..00000000000000 --- a/test/parallel/test-internal-util-toInteger.js +++ /dev/null @@ -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); -}); diff --git a/test/parallel/test-internal-util-toLength.js b/test/parallel/test-internal-util-toLength.js deleted file mode 100644 index ce594c47c1db19..00000000000000 --- a/test/parallel/test-internal-util-toLength.js +++ /dev/null @@ -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); -});