Kewlr
Kewlr
is a module which you can use to determine if two values are equal to each others. It offers two modes - shallow
and strict
, and
it aim to be the fastest deepEqual
algorithm, and support everything that is possible to support.
It should be safe to use this module in production, and it works both for node and the browser.
Note! This module is feature complete, and only bug fixes (if any) will be done
Features
- High performance
- 100% accurate
- Follows ECMA standards
- Works for both NodejS (v. 4.+) and browsers (IE9+)
- shallow, chai and strict mode
Core-js
compatibleChai
compatibleBrowserify
compatible- Babel and Bublé compatible
- fixes all known cross-browser issues
- supports legacy browsers
- supports large data sets
Install
$ npm install --save Kewlr
Usage
Include Kewlr
in your project like this:
// Node 5.x or newer.var strict shallow = ;
or
;
API
Kewlr let you import two different functions, so you can choose between either strict
and shallow
mode when you include this module into your project.
Each of the functions takes two arguments of any type, and returns a boolean result. Primitive types are equal if they are ===
.
While composite types, i.e. Objects
and Arrays
, are considered equal if they have both the same structure and each sub-value is also equal.
Circular references in composite structures are supported.
strict(actual, expected)
actual
- Value to compare againstactual
expected
- Value to compare againstexpected
Returns: Boolean indicating whether or not actual
is the same as expected
.
Same for shallow
and chai
mode.
Differences between shallow and strict mode
The differences between shallow
and strict
mode are mainly the use of tripple equals. And also that the strict mode does a deeply nested sameValue
equality between two objects of any type, and performs a SameValueZero
comparison
between two values to determine if they are equivalent.
chai
mode is is what the name says - turns Kewlr into Chai'r deepEqual module with Kewlrs performance under the hood.
// => true // => false // => true // => false // => true // => false
Examples
Different structure:
// => false // => false
Same structure, different values:
// => false // => false
Primitives:
// => false // => false
Generators
let generator = ; ; // => true; // => true
Symbols()
// => true
Spread operator
let x = ; x;let y = ; y; // => false
Mixed
// false // false // true // true // true // false // false // => true let s = Symbol; ; // true let generator = ; ; // => true; // => true
Legacy browsers
This module should work without any issues with legacy browsers, but require polyfills for ECMA features.
Benchmark
Benchmark comparison against two of the most popular libraries. Chai
is only fast on primitives, and also
contain cross-browser issues.
Lodash
is fast on primitives too, but doesn't support all the same features as Chai
and Kewlr
.
- /NI = Not Implemented
Chai
// ChaiNaN x 22792558 ops/sec ±079% 95 runs sampledstring literal x 50768598 ops/sec ±097% 90 runs sampledarray literal x 328371 ops/sec ±071% 97 runs sampledboolean literal x 4150564 ops/sec ±105% 87 runs sampledobject literal x 146757 ops/sec ±169% 94 runs sampledobject from null x 274394 ops/sec ±130% 94 runs sampledregex literal x 337200 ops/sec ±133% 95 runs sampledString : /NIBoolean : /NINumber : /NINumber literal x 29565083 ops/sec ±108% 90 runs samplednull x 54197476 ops/sec ±167% 94 runs sampledundefined x 53168333 ops/sec ±167% 93 runs sampledbuffer : /NIdate x 353723 ops/sec ±153% 95 runs sampledmap x 344276 ops/sec ±207% 92 runs sampledregex set x 281,988 ops/sec ±1.38% (95 runs sampled)string constructor : /NIarguments x 322,111 ops/sec ±1.71% (87 runs sampled)weakmap x 367,184 ops/sec ±1.37% (92 runs sampled)string literal (differing) x 41,720,813 ops/sec ±0.88% (90 runs sampled)array literal (differing) x 204,749 ops/sec ±1.20% (91 runs sampled)boolean literal (differing) x 41,573,017 ops/sec ±1.61% (93 runs sampled)object literal (differing) x 167,677 ops/sec ±1.66% (95 runs sampled)regex literal (differing) x 343,388 ops/sec ±1.08% (95 runs sampled)number literal (differing) : /NInull & undefined x 47,523,010 ops/sec ±1.09% (94 runs sampled)buffer (differing) x 275,328 ops/sec ±1.88% (93 runs sampled)date (differing) x 352,818 ops/sec ±1.14% (96 runs sampled)number (differing) x 346,692 ops/sec ±1.34% (93 runs sampled)error x 432,609 ops/sec ±1.82% (93 runs sampled)map (differing) x 203,748 ops/sec ±2.00% (93 runs sampled)regex ctor (differing) x 351,938 ops/sec ±1.96% (92 runs sampled)set (differing) x 174,564 ops/sec ±1.65% (91 runs sampled)weakset x 398,499 ops/sec ±1.95% (65 runs sampled)arguments (differing) x 198,811 ops/sec ±1.11% (95 runs sampled)function x 500,304 ops/sec ±1.28% (96 runs sampled)promise x 334,162 ops/sec ±1.94% (86 runs sampled)arrow function (differing) x 636,605 ops/sec ±2.12% (90 runs sampled)generator func (differing) : /NISymbol (differing) x 541,842 ops/sec ±1.68% (84 runs sampled)
Kewlr
NaN x 59709975 ops/sec ±081% 95 runs sampledstring literal x 69543087 ops/sec ±049% 95 runs sampledarray literal x 1089537 ops/sec ±095% 90 runs sampledboolean literal x 60163978 ops/sec ±039% 96 runs sampledobject literal x 930152 ops/sec ±325% 90 runs sampledobject from null x 778688 ops/sec ±064% 97 runs sampledregex literal x 6072546 ops/sec ±125% 96 runs sampledString x 717026 ops/sec ±130% 94 runs sampledBoolean x 981285 ops/sec ±176% 91 runs sampledNumber x 1000314 ops/sec ±252% 87 runs sampledNumber literal x 63529570 ops/sec ±369% 28 runs samplednull x 63308107 ops/sec ±151% 89 runs sampledundefined x 59706867 ops/sec ±111% 92 runs sampledbuffer x 4612967 ops/sec ±137% 95 runs sampleddate x 6198572 ops/sec ±127% 94 runs sampledmap x 944503 ops/sec ±096% 93 runs sampledregex set x 890,192 ops/sec ±2.20% (90 runs sampled)string constructor x 697,697 ops/sec ±0.98% (95 runs sampled)arguments x 589,692 ops/sec ±2.06% (89 runs sampled)string literal (differing) x 31,487,229 ops/sec ±1.79% (58 runs sampled)array literal (differing) x 990,156 ops/sec ±1.42% (92 runs sampled)boolean literal (differing) x 36,656,542 ops/sec ±2.29% (94 runs sampled)object literal (differing) x 720,765 ops/sec ±1.47% (86 runs sampled)regex literal (differing) x 8,193,479 ops/sec ±2.42% (91 runs sampled)null & undefined x 36,199,474 ops/sec ±1.07% (93 runs sampled)buffer (differing) x 4,796,391 ops/sec ±1.18% (97 runs sampled)date (differing) x 6,917,389 ops/sec ±1.73% (90 runs sampled)number (differing) x 684,334 ops/sec ±3.01% (84 runs sampled)error x 1,636,912 ops/sec ±0.98% (85 runs sampled)map (differing) x 787,028 ops/sec ±1.95% (79 runs sampled)regex ctor (differing) x 9,720,439 ops/sec ±2.17% (85 runs sampled)set x 890,192 ops/sec ±2.20% (90 runs sampled)weakmap x 1,614,372 ops/sec ±1.35% (83 runs sampled)weakset x 1,402,493 ops/sec ±1.90% (91 runs sampled)arguments (differing) x 604,466 ops/sec ±1.10% (78 runs sampled)function x 2,506,213 ops/sec ±1.55% (94 runs sampled)promise x 1,377,720 ops/sec ±1.91% (85 runs sampled)arrow function (differing) x 5,070,929 ops/sec ±1.50% (93 runs sampled)generator func (differing) x 1,704,074 ops/sec ±2.40% (92 runs sampled)Symbol (differing) x 33,046,851 ops/sec ±1.43% (96 runs sampled)
Lodash
NaN x 45883884 ops/sec ±124% 96 runs sampledstring literal x 70501125 ops/sec ±128% 91 runs sampledarray literal x 2018210 ops/sec ±096% 95 runs sampledboolean literal x 54537763 ops/sec ±095% 97 runs sampledobject literal x 393746 ops/sec ±166% 56 runs sampledobject from null x 433950 ops/sec ±473% 92 runs sampledregex literal x 479453 ops/sec ±265% 89 runs sampledString x 731572 ops/sec ±100% 95 runs sampledBoolean x 769237 ops/sec ±240% 67 runs sampledNumber x 783885 ops/sec ±150% 94 runs sampledNumber literal x 66340912 ops/sec ±107% 91 runs samplednull x 62093251 ops/sec ±170% 94 runs sampledundefined x 61319209 ops/sec ±200% 69 runs sampledbuffer x 542135 ops/sec ±157% 90 runs sampleddate x 620836 ops/sec ±225% 82 runs sampledmap x 168955 ops/sec ±162% 85 runs sampledregex set x 269,685 ops/sec ±1.25% (87 runs sampled)string constructor x 726,737 ops/sec ±1.74% (85 runs sampled)arguments x 107,957 ops/sec ±1.35% (88 runs sampled)string literal (differing) x 27,173,663 ops/sec ±1.15% (97 runs sampled)array literal (differing) x 1,408,666 ops/sec ±1.14% (97 runs sampled)boolean literal (differing) x 36,127,814 ops/sec ±1.31% (94 runs sampled)object literal (differing) x 422,608 ops/sec ±1.19% (94 runs sampled)regex literal (differing) x 407,979 ops/sec ±2.82% (90 runs sampled)number literal (differing) : /NInull & undefined x 44,806,635 ops/sec ±1.17% (96 runs sampled)buffer (differing) x 976,019 ops/sec ±1.52% (94 runs sampled)date (differing) x 683,029 ops/sec ±1.93% (94 runs sampled)number (differing) : /NInull & undefined x 45,517,618 ops/sec ±2.06% (82 runs sampled)buffer (differing) x 923,542 ops/sec ±1.66% (86 runs sampled)date (differing) x 619,294 ops/sec ±1.60% (88 runs sampled)number (differing) x 663,239 ops/sec ±1.96% (86 runs sampled)error : /NImap (differing) x 184,891 ops/sec ±2.04% (87 runs sampled)regex ctor (differing) x 465,092 ops/sec ±1.24% (84 runs sampled)set (differing) x 346,701 ops/sec ±1.61% (89 runs sampled)weakmap x 1,205,527 ops/sec ±1.89% (86 runs sampled)weakset x 1,267,413 ops/sec ±1.20% (89 runs sampled)arguments (differing) x 130,444 ops/sec ±2.34% (67 runs sampled)function x 1,272,566 ops/sec ±1.80% (93 runs sampled)promise x 1,142,367 ops/sec ±1.39% (86 runs sampled)arrow function (differing) x 1,221,315 ops/sec ±1.00% (87 runs sampled)generator func (differing) x 1,158,629 ops/sec ±1.14% (85 runs sampled)Symbol (differing) x 38,324,415 ops/sec ±1.42% (93 runs sampled)
Chai mode
Kewlr supports a chai
mode. This mode is 100% compatible with Chai's deepEqual module, but with Kewlrs performance under the hood.
Benchmark
- /NI = Not Implemented
// Chai (*original*) NaN x 23135184 ops/sec ±141% 95 runs sampledstring literal x 52196723 ops/sec ±032% 96 runs sampledarray literal x 391719 ops/sec ±256% 95 runs sampledboolean literal x 43430996 ops/sec ±152% 68 runs sampledobject literal x 218613 ops/sec ±196% 94 runs sampledobject from null x 303973 ops/sec ±216% 76 runs sampledregex literal x 321041 ops/sec ±117% 96 runs sampledString : /NIBoolean : /NINumber : /NINumber literal x 29031506 ops/sec ±124% 96 runs samplednull x 42235260 ops/sec ±144% 57 runs sampledundefined x 47488519 ops/sec ±092% 98 runs sampledbuffer : /NIdate x 359681 ops/sec ±250% 88 runs sampledmap x 325956 ops/sec ±191% 59 runs sampledregex set x 281,116 ops/sec ±1.61% (88 runs sampled)string constructor : /NIarguments x 328,653 ops/sec ±1.69% (96 runs sampled)promise x 391,043 ops/sec ±2.25% (92 runs sampled)weakmap x 422,815 ops/sec ±1.32% (96 runs sampled)string literal (differing) x 38,615,393 ops/sec ±1.85% (95 runs sampled)array literal (differing) x 207,455 ops/sec ±1.78% (91 runs sampled)boolean literal (differing) x 42,698,174 ops/sec ±1.20% (95 runs sampled)object literal (differing) x 128,334 ops/sec ±2.27% (90 runs sampled)regex literal (differing) x 304,894 ops/sec ±1.54% (93 runs sampled)number literal (differing) : /NInull & undefined x 44,412,001 ops/sec ±1.13% (92 runs sampled)buffer (differing) x 307,443 ops/sec ±1.57% (94 runs sampled)date (differing) x 360,143 ops/sec ±1.34% (94 runs sampled)number (differing) x 340,573 ops/sec ±1.77% (90 runs sampled)error x 415,895 ops/sec ±1.06% (96 runs sampled)map (differing) x 191,026 ops/sec ±2.30% (91 runs sampled)regex ctor (differing) x 321,128 ops/sec ±0.76% (58 runs sampled)set (differing) x 169,544 ops/sec ±1.40% (94 runs sampled)string ctor (differing) x 312,103 ops/sec ±1.40% (93 runs sampled)weakset x 314,585 ops/sec ±1.25% (93 runs sampled)arguments (differing) x 205,860 ops/sec ±1.83% (90 runs sampled)function x 450,480 ops/sec ±1.91% (93 runs sampled)arrow function (differing) x 583,289 ops/sec ±3.84% (66 runs sampled)generator func (differing) : /NISymbol (differing) x 536,168 ops/sec ±1.79% (91 runs sampled) // Kewlr - Chai mode NaN x 46,946,316 ops/sec ±0.50% (95 runs sampled)string literal x 50,674,400 ops/sec ±0.31% (98 runs sampled)array literal x 1,027,384 ops/sec ±0.65% (94 runs sampled)boolean literal x 44,101,352 ops/sec ±0.63% (97 runs sampled)object literal x 845,436 ops/sec ±1.13% (98 runs sampled)object from null x 544,655 ops/sec ±0.64% (92 runs sampled)regex literal x 5,934,119 ops/sec ±0.47% (98 runs sampled)String x 688,034 ops/sec ±2.59% (92 runs sampled)Boolean x 929,798 ops/sec ±0.69% (96 runs sampled)Number x 897,256 ops/sec ±0.57% (92 runs sampled)Number literal x 48,896,797 ops/sec ±0.85% (95 runs sampled)null x 48,032,197 ops/sec ±2.85% (58 runs sampled)undefined x 47,351,088 ops/sec ±1.16% (97 runs sampled)buffer : /NIdate x 8,181,286 ops/sec ±0.67% (96 runs sampled)map x 932,079 ops/sec ±0.66% (94 runs sampled)regex constructor x 6,296,232 ops/sec ±0.40% (95 runs sampled)set x 907,274 ops/sec ±0.87% (95 runs sampled)string constructor x 691,929 ops/sec ±1.62% (93 runs sampled)arguments x 576,035 ops/sec ±0.80% (95 runs sampled)promise x 999,637 ops/sec ±0.76% (94 runs sampled)weakmap x 1,164,366 ops/sec ±0.63% (96 runs sampled)string literal (differing) x 38,990,103 ops/sec ±0.45% (93 runs sampled)array literal (differing) x 894,618 ops/sec ±1.57% (94 runs sampled)boolean literal (differing) x 32,678,290 ops/sec ±1.87% (96 runs sampled)object literal (differing) x 541,684 ops/sec ±1.05% (95 runs sampled)regex literal (differing) x 8,576,171 ops/sec ±0.45% (99 runs sampled)number literal (differing) : /NInull & undefined x 44,298,328 ops/sec ±0.56% (97 runs sampled)buffer (differing) x 4,888,593 ops/sec ±1.59% (95 runs sampled)date (differing) x 8,095,821 ops/sec ±0.62% (96 runs sampled)number (differing) x 775,817 ops/sec ±2.33% (89 runs sampled)error x 1,262,244 ops/sec ±1.62% (93 runs sampled)map (differing) x 949,984 ops/sec ±1.80% (95 runs sampled)regex ctor (differing) x 8,176,987 ops/sec ±2.31% (92 runs sampled)set (differing) x 857,345 ops/sec ±1.45% (92 runs sampled)string ctor (differing) : /NIweakset x 1,110,013 ops/sec ±0.76% (96 runs sampled)arguments (differing) x 466,570 ops/sec ±1.13% (92 runs sampled)function x 2,184,845 ops/sec ±1.63% (85 runs sampled)arrow function (differing) x 5,024,794 ops/sec ±1.70% (95 runs sampled)generator func (differing) x 2,241,420 ops/sec ±1.65% (95 runs sampled)Symbol (differing) x 40,403,549 ops/sec ±2.10% (61 runs sampled)