Skip to content

Commit e8b4c0b

Browse files
assert: avoid excess float array comparisons
Signed-off-by: alejandrodev-vn <huytra264@gmail.com>
1 parent f344e7f commit e8b4c0b

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const assert = require('node:assert');
4+
5+
const bench = common.createBenchmark(main, {
6+
type: ['Float16Array', 'Float32Array', 'Float64Array'],
7+
len: [0, 1, 100, 5000],
8+
scenario: ['equal', 'different_first', 'different_last', 'different_length'],
9+
n: [10000],
10+
}, {
11+
combinationFilter({ len, scenario }) {
12+
return len !== 0 || scenario === 'equal' || scenario === 'different_length';
13+
},
14+
});
15+
16+
function main({ type, len, scenario, n }) {
17+
const clazz = globalThis[type];
18+
const actual = new clazz(len);
19+
const expected = new clazz(len + Number(scenario === 'different_length'));
20+
actual.fill(1.5);
21+
expected.fill(1.5);
22+
23+
if (len > 0 && scenario === 'different_first') {
24+
expected[0] = 2.5;
25+
} else if (len > 0 && scenario === 'different_last') {
26+
expected[len - 1] = 2.5;
27+
}
28+
29+
const fn = scenario === 'equal' ?
30+
// eslint-disable-next-line no-restricted-properties
31+
assert.deepEqual : assert.notDeepEqual;
32+
33+
bench.start();
34+
for (let i = 0; i < n; i++) {
35+
fn(actual, expected);
36+
}
37+
bench.end(n);
38+
}

‎lib/internal/util/comparisons.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
SymbolPrototypeValueOf,
4545
SymbolToStringTag,
4646
TypedArrayPrototypeGetByteLength: getByteLength,
47+
TypedArrayPrototypeGetLength,
4748
TypedArrayPrototypeGetSymbolToStringTag,
4849
Uint16Array,
4950
Uint32Array,
@@ -181,8 +182,8 @@ function isPartialArrayBufferView(a, b) {
181182
}
182183

183184
function areSimilarFloatArrays(a, b) {
184-
const len = getByteLength(a);
185-
if (len !== getByteLength(b)) {
185+
const len = TypedArrayPrototypeGetLength(a);
186+
if (len !== TypedArrayPrototypeGetLength(b)) {
186187
return false;
187188
}
188189
for (let offset = 0; offset < len; offset++) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('node:assert');
5+
6+
for (const FloatArray of [Float16Array, Float32Array, Float64Array]) {
7+
const actual = new FloatArray([0, 1.5, 2.5, 0]).subarray(1, 3);
8+
const equal = new FloatArray([1.5, 2.5]);
9+
const different = new FloatArray([1.5, 3.5]);
10+
const shorter = new FloatArray([1.5]);
11+
const empty = new FloatArray(0);
12+
const emptyEqual = new FloatArray(0);
13+
14+
// Own properties must not replace the view's intrinsic element count.
15+
for (const view of [actual, equal, different, shorter, empty, emptyEqual]) {
16+
Object.defineProperty(view, 'length', {
17+
get: common.mustNotCall('length getter'),
18+
});
19+
Object.defineProperty(view, 'byteLength', {
20+
get: common.mustNotCall('byteLength getter'),
21+
});
22+
}
23+
24+
// eslint-disable-next-line no-restricted-properties
25+
assert.deepEqual(actual, equal);
26+
// eslint-disable-next-line no-restricted-properties
27+
assert.notDeepEqual(actual, different);
28+
// eslint-disable-next-line no-restricted-properties
29+
assert.notDeepEqual(actual, shorter);
30+
// eslint-disable-next-line no-restricted-properties
31+
assert.notDeepEqual(actual, empty);
32+
// eslint-disable-next-line no-restricted-properties
33+
assert.deepEqual(empty, emptyEqual);
34+
}

0 commit comments

Comments
 (0)