Skip to content

@substack /gist:2790507
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
assert.deepEqual() vs require('deep-equal')
> var eq = require('deep-equal'); var obj1 = { a : 2, b : 3, c : [ 3, 4 ] }, obj2 = { a : 2, b : 3, c : [ 4, 5 ] }; var t0 = Date.now(); for (var i = 0; i < 10000; i++) eq(obj1, obj2); Date.now() - t0
125
> var assert = require('assert'); function eq (x, y) { try { assert.deepEqual(x,y); return true } catch (e) { return false } } var obj1 = { a : 2, b : 3, c : [ 3, 4 ] }, obj2 = { a : 2, b : 3, c : [ 4, 5 ] }; var t0 = Date.now(); for (var i = 0; i < 10000; i++) eq(obj1, obj2); Date.now() - t0
566
@defunctzombie

The deep-equal doesn't use the try/catch so that may be causing some penalty with the actual benchmark. What does it look like if you don't have the try/catch?

@substack
Owner

The try/catch is the benchmark. The deep-equal package is the assert.deepEqual source except that it returns true or false instead of throwing.

@malandrew

For the sake of contributing another data point, deep-equal is only marginally faster when the objects being compared are very large, such as testing the JSON representation of scraping an entire page. I compared a 5kb json file with itself and the results were 801 and 862, respectively.

FYI, on my machine, I got 41 and 155 for the two tests above. (3x)

@malandrew

FWIW, what would be valuable versus node's assert.deepEqual function would be a deepEqual that performs a diff of the two objects and tells you exactly how they are different. I guess such a function would best be called deepDiff().

@connor4312

Also of note (if you're not sure of the types of things your comparing) deep-equal is significantly more efficient on simple data types, such as integers.

> var eq = require('deep-equal'); var obj1 = 1, obj2 = 2; var t0 = Date.now(); for (var i = 0; i < 10000; i++) eq(obj1, obj2); Date.now() - t0
1
> var assert = require('assert'); function eq (x, y) { try { assert.deepEqual(x,y); return true } catch (e) { return false } } var obj1 = 1, obj2 = 2; var t0 = Date.now(); for (var i = 0; i < 10000; i++) eq(obj1, obj2); Date.now() - t0
116
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.