Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions benchmark/assert/deepequal-floatarrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
const common = require('../common.js');
const assert = require('node:assert');

const bench = common.createBenchmark(main, {
type: ['Float16Array', 'Float32Array', 'Float64Array'],
len: [0, 1, 100, 5000],
scenario: ['equal', 'different_first', 'different_last', 'different_length'],
n: [10000],
}, {
combinationFilter({ len, scenario }) {
return len !== 0 || scenario === 'equal' || scenario === 'different_length';
},
});

function main({ type, len, scenario, n }) {
const clazz = globalThis[type];
const actual = new clazz(len);
const expected = new clazz(len + Number(scenario === 'different_length'));
actual.fill(1.5);
expected.fill(1.5);

if (len > 0 && scenario === 'different_first') {
expected[0] = 2.5;
} else if (len > 0 && scenario === 'different_last') {
expected[len - 1] = 2.5;
}

const fn = scenario === 'equal' ?
// eslint-disable-next-line no-restricted-properties
assert.deepEqual : assert.notDeepEqual;

bench.start();
for (let i = 0; i < n; i++) {
fn(actual, expected);
}
bench.end(n);
}
5 changes: 3 additions & 2 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const {
SymbolPrototypeValueOf,
SymbolToStringTag,
TypedArrayPrototypeGetByteLength: getByteLength,
TypedArrayPrototypeGetLength,
TypedArrayPrototypeGetSymbolToStringTag,
Uint16Array,
Uint32Array,
Expand Down Expand Up @@ -181,8 +182,8 @@ function isPartialArrayBufferView(a, b) {
}

function areSimilarFloatArrays(a, b) {
const len = getByteLength(a);
if (len !== getByteLength(b)) {
const len = TypedArrayPrototypeGetLength(a);
if (len !== TypedArrayPrototypeGetLength(b)) {
return false;
}
for (let offset = 0; offset < len; offset++) {
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-assert-deep-equal-floatarray-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const common = require('../common');
const assert = require('node:assert');

for (const FloatArray of [Float16Array, Float32Array, Float64Array]) {
const actual = new FloatArray([0, 1.5, 2.5, 0]).subarray(1, 3);
const equal = new FloatArray([1.5, 2.5]);
const different = new FloatArray([1.5, 3.5]);
const shorter = new FloatArray([1.5]);
const empty = new FloatArray(0);
const emptyEqual = new FloatArray(0);

// Check strict comparisons before installing the loose-mode getter guards.
assert.deepStrictEqual(actual, equal);
assert.notDeepStrictEqual(actual, different);
assert.notDeepStrictEqual(actual, shorter);
assert.notDeepStrictEqual(actual, empty);
assert.deepStrictEqual(empty, emptyEqual);

// Own properties must not replace the view's intrinsic element count.
for (const view of [actual, equal, different, shorter, empty, emptyEqual]) {
Object.defineProperty(view, 'length', {
get: common.mustNotCall('length getter'),
});
Object.defineProperty(view, 'byteLength', {
get: common.mustNotCall('byteLength getter'),
});
}

// eslint-disable-next-line no-restricted-properties
assert.deepEqual(actual, equal);
// eslint-disable-next-line no-restricted-properties
assert.notDeepEqual(actual, different);
// eslint-disable-next-line no-restricted-properties
assert.notDeepEqual(actual, shorter);
// eslint-disable-next-line no-restricted-properties
assert.notDeepEqual(actual, empty);
// eslint-disable-next-line no-restricted-properties
assert.deepEqual(empty, emptyEqual);
}
Loading