From 74db9f43ba0cd451af165b820b59c336814fe6f2 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 7 May 2018 03:53:02 +0200 Subject: [PATCH] assert,util: lazy load comparison functions PR-URL: https://github.com/nodejs/node/pull/20567 Reviewed-By: Gus Caplan Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/assert.js | 18 ++++++++++++++---- lib/util.js | 13 ++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 8ed90608d8f88d..98900c5151ed41 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -21,10 +21,6 @@ 'use strict'; const { Buffer } = require('buffer'); -const { - isDeepEqual, - isDeepStrictEqual -} = require('internal/util/comparisons'); const { codes: { ERR_AMBIGUOUS_ARGUMENT, ERR_INVALID_ARG_TYPE, @@ -36,6 +32,15 @@ const { inspect, types: { isPromise, isRegExp } } = require('util'); const { EOL } = require('internal/constants'); const { NativeModule } = require('internal/bootstrap/loaders'); +let isDeepEqual; +let isDeepStrictEqual; + +function lazyLoadComparison() { + const comparison = require('internal/util/comparisons'); + isDeepEqual = comparison.isDeepEqual; + isDeepStrictEqual = comparison.isDeepStrictEqual; +} + // Escape control characters but not \n and \t to keep the line breaks and // indentation intact. // eslint-disable-next-line no-control-regex @@ -284,6 +289,7 @@ assert.notEqual = function notEqual(actual, expected, message) { // The equivalence assertion tests a deep equality relation. assert.deepEqual = function deepEqual(actual, expected, message) { + if (isDeepEqual === undefined) lazyLoadComparison(); if (!isDeepEqual(actual, expected)) { innerFail({ actual, @@ -297,6 +303,7 @@ assert.deepEqual = function deepEqual(actual, expected, message) { // The non-equivalence assertion tests for any deep inequality. assert.notDeepEqual = function notDeepEqual(actual, expected, message) { + if (isDeepEqual === undefined) lazyLoadComparison(); if (isDeepEqual(actual, expected)) { innerFail({ actual, @@ -310,6 +317,7 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) { /* eslint-enable */ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { + if (isDeepEqual === undefined) lazyLoadComparison(); if (!isDeepStrictEqual(actual, expected)) { innerFail({ actual, @@ -323,6 +331,7 @@ assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { assert.notDeepStrictEqual = notDeepStrictEqual; function notDeepStrictEqual(actual, expected, message) { + if (isDeepEqual === undefined) lazyLoadComparison(); if (isDeepStrictEqual(actual, expected)) { innerFail({ actual, @@ -439,6 +448,7 @@ function expectedException(actual, expected, msg) { if (expected instanceof Error) { keys.push('name', 'message'); } + if (isDeepEqual === undefined) lazyLoadComparison(); for (const key of keys) { if (typeof actual[key] === 'string' && isRegExp(expected[key]) && diff --git a/lib/util.js b/lib/util.js index 45d98de194c836..bd7a98694b91a9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -64,10 +64,6 @@ const { isTypedArray } = types; -const { - isDeepStrictEqual -} = require('internal/util/comparisons'); - const { customInspectSymbol, deprecate, @@ -95,6 +91,7 @@ const dateToISOString = Date.prototype.toISOString; const errorToString = Error.prototype.toString; let CIRCULAR_ERROR_MESSAGE; +let internalDeepEqual; /* eslint-disable */ const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/; @@ -1261,7 +1258,13 @@ module.exports = exports = { isArray: Array.isArray, isBoolean, isBuffer, - isDeepStrictEqual, + isDeepStrictEqual(a, b) { + if (internalDeepEqual === undefined) { + internalDeepEqual = require('internal/util/comparisons') + .isDeepStrictEqual; + } + return internalDeepEqual(a, b); + }, isNull, isNullOrUndefined, isNumber,