|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | +const assert = require('assert'); |
| 5 | + |
| 6 | +const bench = common.createBenchmark(main, { |
| 7 | + n: [10, 50, 200], |
| 8 | + size: [1e3], |
| 9 | + datasetName: [ |
| 10 | + 'objects', |
| 11 | + 'sets', |
| 12 | + 'maps', |
| 13 | + 'circularRefs', |
| 14 | + 'typedArrays', |
| 15 | + 'arrayBuffers', |
| 16 | + 'dataViewArrayBuffers', |
| 17 | + ], |
| 18 | +}); |
| 19 | + |
| 20 | +function createObjects(length, depth = 0) { |
| 21 | + return Array.from({ length }, () => ({ |
| 22 | + foo: 'yarp', |
| 23 | + nope: { |
| 24 | + bar: '123', |
| 25 | + a: [1, 2, 3], |
| 26 | + c: {}, |
| 27 | + b: !depth ? createObjects(2, depth + 1) : [], |
| 28 | + }, |
| 29 | + })); |
| 30 | +} |
| 31 | + |
| 32 | +function createSets(length, depth = 0) { |
| 33 | + return Array.from({ length }, () => new Set([ |
| 34 | + 'yarp', |
| 35 | + '123', |
| 36 | + 1, |
| 37 | + 2, |
| 38 | + 3, |
| 39 | + null, |
| 40 | + { |
| 41 | + simple: 'object', |
| 42 | + number: 42, |
| 43 | + }, |
| 44 | + ['array', 'with', 'values'], |
| 45 | + !depth ? new Set([1, 2, { nested: true }]) : new Set(), |
| 46 | + !depth ? createSets(2, depth + 1) : null, |
| 47 | + ])); |
| 48 | +} |
| 49 | + |
| 50 | +function createMaps(length, depth = 0) { |
| 51 | + return Array.from({ length }, () => new Map([ |
| 52 | + ['primitiveKey', 'primitiveValue'], |
| 53 | + [42, 'numberKey'], |
| 54 | + ['objectValue', { a: 1, b: 2 }], |
| 55 | + ['arrayValue', [1, 2, 3]], |
| 56 | + ['nestedMap', new Map([['a', 1], ['b', { deep: true }]])], |
| 57 | + [{ objectKey: true }, 'value from object key'], |
| 58 | + [[1, 2, 3], 'value from array key'], |
| 59 | + [!depth ? createMaps(2, depth + 1) : null, 'recursive value'], |
| 60 | + ])); |
| 61 | +} |
| 62 | + |
| 63 | +function createCircularRefs(length) { |
| 64 | + return Array.from({ length }, () => { |
| 65 | + const circularSet = new Set(); |
| 66 | + const circularMap = new Map(); |
| 67 | + const circularObj = { name: 'circular object' }; |
| 68 | + |
| 69 | + circularSet.add('some value'); |
| 70 | + circularSet.add(circularSet); |
| 71 | + |
| 72 | + circularMap.set('self', circularMap); |
| 73 | + circularMap.set('value', 'regular value'); |
| 74 | + |
| 75 | + circularObj.self = circularObj; |
| 76 | + |
| 77 | + const objA = { name: 'A' }; |
| 78 | + const objB = { name: 'B' }; |
| 79 | + objA.ref = objB; |
| 80 | + objB.ref = objA; |
| 81 | + |
| 82 | + circularSet.add(objA); |
| 83 | + circularMap.set('objB', objB); |
| 84 | + |
| 85 | + return { |
| 86 | + circularSet, |
| 87 | + circularMap, |
| 88 | + circularObj, |
| 89 | + objA, |
| 90 | + objB, |
| 91 | + }; |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +function createTypedArrays(length) { |
| 96 | + return Array.from({ length }, () => { |
| 97 | + const buffer = new ArrayBuffer(32); |
| 98 | + |
| 99 | + return { |
| 100 | + int8: new Int8Array(buffer, 0, 4), |
| 101 | + uint8: new Uint8Array(buffer, 4, 4), |
| 102 | + uint8Clamped: new Uint8ClampedArray(buffer, 8, 4), |
| 103 | + int16: new Int16Array([1, 2, 3]), |
| 104 | + uint16: new Uint16Array([1, 2, 3]), |
| 105 | + int32: new Int32Array([1, 2, 3]), |
| 106 | + uint32: new Uint32Array([1, 2, 3]), |
| 107 | + float32: new Float32Array([1.1, 2.2, 3.3]), |
| 108 | + float64: new Float64Array([1.1, 2.2, 3.3]), |
| 109 | + bigInt64: new BigInt64Array([1n, 2n, 3n]), |
| 110 | + bigUint64: new BigUint64Array([1n, 2n, 3n]), |
| 111 | + }; |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +function createArrayBuffers(length) { |
| 116 | + return Array.from({ length }, (_, n) => new ArrayBuffer(n)); |
| 117 | +} |
| 118 | + |
| 119 | +function createDataViewArrayBuffers(length) { |
| 120 | + return Array.from({ length }, (_, n) => new DataView(new ArrayBuffer(n))); |
| 121 | +} |
| 122 | + |
| 123 | +const datasetMappings = { |
| 124 | + objects: createObjects, |
| 125 | + sets: createSets, |
| 126 | + maps: createMaps, |
| 127 | + circularRefs: createCircularRefs, |
| 128 | + typedArrays: createTypedArrays, |
| 129 | + arrayBuffers: createArrayBuffers, |
| 130 | + dataViewArrayBuffers: createDataViewArrayBuffers, |
| 131 | +}; |
| 132 | + |
| 133 | +function getDatasets(datasetName, size) { |
| 134 | + return { |
| 135 | + actual: datasetMappings[datasetName](size), |
| 136 | + expected: datasetMappings[datasetName](size), |
| 137 | + }; |
| 138 | +} |
| 139 | + |
| 140 | +function main({ size, n, datasetName }) { |
| 141 | + const { actual, expected } = getDatasets(datasetName, size); |
| 142 | + |
| 143 | + bench.start(); |
| 144 | + for (let i = 0; i < n; ++i) { |
| 145 | + assert.partialDeepStrictEqual(actual, expected); |
| 146 | + } |
| 147 | + bench.end(n); |
| 148 | +} |
0 commit comments