Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 901172a

Browse filesBrowse files
claudiorodriguezMyles Borins
authored andcommitted
assert: typed array deepequal performance fix
assert.deepEqual: when actual and expected are typed arrays, wrap them in a new Buffer each to increase performance significantly. PR-URL: #4330 Fixes: #4294 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 138d004 commit 901172a
Copy full SHA for 901172a

File tree

Expand file treeCollapse file tree

5 files changed

+135
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+135
-0
lines changed
Open diff view settings
Collapse file
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
var common = require('../common.js');
3+
var assert = require('assert');
4+
var bench = common.createBenchmark(main, {
5+
prim: [
6+
null,
7+
undefined,
8+
'a',
9+
1,
10+
true,
11+
{0: 'a'},
12+
[1, 2, 3],
13+
new Array([1, 2, 3])
14+
],
15+
n: [25]
16+
});
17+
18+
function main(conf) {
19+
var prim = conf.prim;
20+
var n = +conf.n;
21+
var primArray;
22+
var primArrayCompare;
23+
var x;
24+
25+
primArray = new Array();
26+
primArrayCompare = new Array();
27+
for (x = 0; x < (1e5); x++) {
28+
primArray.push(prim);
29+
primArrayCompare.push(prim);
30+
}
31+
32+
bench.start();
33+
for (x = 0; x < n; x++) {
34+
assert.deepEqual(primArray, primArrayCompare);
35+
}
36+
bench.end(n);
37+
}
Collapse file
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
var common = require('../common.js');
3+
var assert = require('assert');
4+
var bench = common.createBenchmark(main, {
5+
prim: [
6+
null,
7+
undefined,
8+
'a',
9+
1,
10+
true,
11+
{0: 'a'},
12+
[1, 2, 3],
13+
new Array([1, 2, 3])
14+
],
15+
n: [1e5]
16+
});
17+
18+
function main(conf) {
19+
var prim = conf.prim;
20+
var n = +conf.n;
21+
var x;
22+
23+
bench.start();
24+
25+
for (x = 0; x < n; x++) {
26+
assert.deepEqual(new Array([prim]), new Array([prim]));
27+
}
28+
29+
bench.end(n);
30+
}
Collapse file
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
var common = require('../common.js');
3+
var assert = require('assert');
4+
var bench = common.createBenchmark(main, {
5+
type: ('Int8Array Uint8Array Int16Array Uint16Array Int32Array Uint32Array ' +
6+
'Float32Array Float64Array Uint8ClampedArray').split(' '),
7+
n: [1]
8+
});
9+
10+
function main(conf) {
11+
var type = conf.type;
12+
var clazz = global[type];
13+
var n = +conf.n;
14+
15+
bench.start();
16+
var actual = new clazz(n * 1e6);
17+
var expected = new clazz(n * 1e6);
18+
19+
assert.deepEqual(actual, expected);
20+
21+
bench.end(n);
22+
}
Collapse file

‎lib/assert.js‎

Copy file name to clipboardExpand all lines: lib/assert.js
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ function _deepEqual(actual, expected, strict) {
170170
(expected === null || typeof expected !== 'object')) {
171171
return strict ? actual === expected : actual == expected;
172172

173+
// If both values are instances of typed arrays, wrap them in
174+
// a Buffer each to increase performance
175+
} else if (ArrayBuffer.isView(actual) && ArrayBuffer.isView(expected)) {
176+
return compare(new Buffer(actual), new Buffer(expected)) === 0;
177+
173178
// 7.5 For all other Object pairs, including Array objects, equivalence is
174179
// determined by having the same number of owned properties (as verified
175180
// with Object.prototype.hasOwnProperty.call), the same set of keys
Collapse file
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const a = require('assert');
6+
7+
function makeBlock(f) {
8+
var args = Array.prototype.slice.call(arguments, 1);
9+
return function() {
10+
return f.apply(this, args);
11+
};
12+
}
13+
14+
const equalArrayPairs = [
15+
[new Uint8Array(1e5), new Uint8Array(1e5)],
16+
[new Uint16Array(1e5), new Uint16Array(1e5)],
17+
[new Uint32Array(1e5), new Uint32Array(1e5)],
18+
[new Uint8ClampedArray(1e5), new Uint8ClampedArray(1e5)],
19+
[new Int8Array(1e5), new Int8Array(1e5)],
20+
[new Int16Array(1e5), new Int16Array(1e5)],
21+
[new Int32Array(1e5), new Int32Array(1e5)],
22+
[new Float32Array(1e5), new Float32Array(1e5)],
23+
[new Float64Array(1e5), new Float64Array(1e5)]
24+
];
25+
26+
const notEqualArrayPairs = [
27+
[new Uint8Array(2), new Uint8Array(3)],
28+
[new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])],
29+
[new Uint8ClampedArray([300, 2, 3]), new Uint8Array([300, 2, 3])]
30+
];
31+
32+
equalArrayPairs.forEach((arrayPair) => {
33+
assert.deepEqual(arrayPair[0], arrayPair[1]);
34+
});
35+
36+
notEqualArrayPairs.forEach((arrayPair) => {
37+
assert.throws(
38+
makeBlock(a.deepEqual, arrayPair[0], arrayPair[1]),
39+
a.AssertionError
40+
);
41+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.