public
Created

assert.deepEqual() vs require('deep-equal')

  • Download Gist
gistfile1.txt
1 2 3 4
> 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

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?

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.

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)

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().

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.