summaryrefslogtreecommitdiff
path: root/benchmark/assert/deepequal-prims-and-objs-big-loop.js
blob: 1ab4ff4dd81f338b0fe18de6578ffa34efec293a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
const common = require('../common.js');
const assert = require('assert');

const circular = {};
circular.circular = circular;
const circular2 = {};
circular2.circular = circular2;
const notCircular = {};
notCircular.circular = {};

const primValues = {
  'string': 'abcdef',
  'number': 1_000,
  'boolean': true,
  'object': { property: 'abcdef' },
  'object_other_property': { property: 'abcdef' },
  'array': [1, 2, 3],
  'set_object': new Set([[1]]),
  'set_simple': new Set([1, 2, 3]),
  'circular': circular,
  'empty_object': {},
  'regexp': /abc/i,
  'date': new Date(),
};

const primValues2 = {
  'object': { property: 'abcdef' },
  'array': [1, 2, 3],
  'set_object': new Set([[1]]),
  'set_simple': new Set([1, 3, 2]),
  'circular': circular2,
  'empty_object': {},
  'regexp': /abc/i,
  'date': new Date(primValues.date),
};

const primValuesUnequal = {
  'string': 'abcdez',
  'number': 1_001,
  'boolean': false,
  'object': { property2: 'abcdef' },
  'array': [1, 3, 2],
  'set_object': new Set([[2]]),
  'set_simple': new Set([1, 4, 2]),
  'circular': notCircular,
  'empty_object': [],
  'regexp': /abc/g,
  'date': new Date(primValues.date.getTime() + 1),
};

const bench = common.createBenchmark(main, {
  primitive: Object.keys(primValues),
  n: [1e5],
  strict: [0, 1],
  method: ['deepEqual', 'notDeepEqual'],
}, {
  combinationFilter: (p) => {
    return p.strict === 1 || p.method === 'deepEqual';
  },
});

function main({ n, primitive, method, strict }) {
  const prim = primValues[primitive];
  const actual = primValues2[primitive] ?? prim;
  const expected = method.includes('not') ? primValuesUnequal[primitive] : prim;

  if (strict) {
    method = method.replace('eep', 'eepStrict');
  }
  const fn = assert[method];

  bench.start();
  for (let i = 0; i < n; ++i) {
    fn(actual, expected);
  }
  bench.end(n);
}