diff --git a/lib/assert.js b/lib/assert.js index 06b4815ea6cfe6..6f340d8ef4e46b 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -21,10 +21,8 @@ 'use strict'; const { compare } = process.binding('buffer'); -const util = require('util'); -const { isSet, isMap } = process.binding('util'); +const { isSet, isMap, isDate, isRegExp } = process.binding('util'); const { objectToString } = require('internal/util'); -const { Buffer } = require('buffer'); const errors = require('internal/errors'); // The assert module provides functions that throw @@ -108,8 +106,8 @@ function areSimilarRegExps(a, b) { } // For small buffers it's faster to compare the buffer in a loop. The c++ -// barrier including the Buffer.from operation takes the advantage of the faster -// compare otherwise. 300 was the number after which compare became faster. +// barrier including the Uint8Array operation takes the advantage of the faster +// binary compare otherwise. The break even point was at about 300 characters. function areSimilarTypedArrays(a, b) { const len = a.byteLength; if (len !== b.byteLength) { @@ -123,12 +121,8 @@ function areSimilarTypedArrays(a, b) { } return true; } - return compare(Buffer.from(a.buffer, - a.byteOffset, - len), - Buffer.from(b.buffer, - b.byteOffset, - b.byteLength)) === 0; + return compare(new Uint8Array(a.buffer, a.byteOffset, len), + new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0; } function isFloatTypedArrayTag(tag) { @@ -189,11 +183,11 @@ function strictDeepEqual(actual, expected) { // Skip testing the part below and continue in the callee function. return; } - if (util.isDate(actual)) { + if (isDate(actual)) { if (actual.getTime() !== expected.getTime()) { return false; } - } else if (util.isRegExp(actual)) { + } else if (isRegExp(actual)) { if (!areSimilarRegExps(actual, expected)) { return false; } @@ -229,10 +223,10 @@ function looseDeepEqual(actual, expected) { if (expected === null || typeof expected !== 'object') { return false; } - if (util.isDate(actual) && util.isDate(expected)) { + if (isDate(actual) && isDate(expected)) { return actual.getTime() === expected.getTime(); } - if (util.isRegExp(actual) && util.isRegExp(expected)) { + if (isRegExp(actual) && isRegExp(expected)) { return areSimilarRegExps(actual, expected); } if (actual instanceof Error && expected instanceof Error) { diff --git a/lib/util.js b/lib/util.js index 03561bcb0914f6..a2c8a6436f837a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -23,6 +23,7 @@ const errors = require('internal/errors'); const { TextDecoder, TextEncoder } = require('internal/encoding'); +const { isBuffer } = require('buffer').Buffer; const { errname } = process.binding('uv'); @@ -1118,6 +1119,7 @@ module.exports = exports = { inspect, isArray: Array.isArray, isBoolean, + isBuffer, isNull, isNullOrUndefined, isNumber, @@ -1149,18 +1151,3 @@ module.exports = exports = { 'util.puts is deprecated. Use console.log instead.', 'DEP0027') }; - -// Avoid a circular dependency -var isBuffer; -Object.defineProperty(exports, 'isBuffer', { - configurable: true, - enumerable: true, - get() { - if (!isBuffer) - isBuffer = require('buffer').Buffer.isBuffer; - return isBuffer; - }, - set(val) { - isBuffer = val; - } -});