11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common.js');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  method: [
81cb0ef41Sopenharmony_ci    'object', 'nullProtoObject', 'nullProtoLiteralObject', 'storageObject',
91cb0ef41Sopenharmony_ci    'fakeMap', 'map',
101cb0ef41Sopenharmony_ci  ],
111cb0ef41Sopenharmony_ci  n: [1e6],
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction runObject(n) {
151cb0ef41Sopenharmony_ci  const m = {};
161cb0ef41Sopenharmony_ci  bench.start();
171cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
181cb0ef41Sopenharmony_ci    m[`i${i}`] = i;
191cb0ef41Sopenharmony_ci    m[`s${i}`] = String(i);
201cb0ef41Sopenharmony_ci    assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
211cb0ef41Sopenharmony_ci    m[`i${i}`] = undefined;
221cb0ef41Sopenharmony_ci    m[`s${i}`] = undefined;
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci  bench.end(n);
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cifunction runNullProtoObject(n) {
281cb0ef41Sopenharmony_ci  const m = Object.create(null);
291cb0ef41Sopenharmony_ci  bench.start();
301cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
311cb0ef41Sopenharmony_ci    m[`i${i}`] = i;
321cb0ef41Sopenharmony_ci    m[`s${i}`] = String(i);
331cb0ef41Sopenharmony_ci    assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
341cb0ef41Sopenharmony_ci    m[`i${i}`] = undefined;
351cb0ef41Sopenharmony_ci    m[`s${i}`] = undefined;
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci  bench.end(n);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifunction runNullProtoLiteralObject(n) {
411cb0ef41Sopenharmony_ci  const m = { __proto__: null };
421cb0ef41Sopenharmony_ci  bench.start();
431cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
441cb0ef41Sopenharmony_ci    m[`i${i}`] = i;
451cb0ef41Sopenharmony_ci    m[`s${i}`] = String(i);
461cb0ef41Sopenharmony_ci    assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
471cb0ef41Sopenharmony_ci    m[`i${i}`] = undefined;
481cb0ef41Sopenharmony_ci    m[`s${i}`] = undefined;
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci  bench.end(n);
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cifunction StorageObject() {}
541cb0ef41Sopenharmony_ciStorageObject.prototype = Object.create(null);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cifunction runStorageObject(n) {
571cb0ef41Sopenharmony_ci  const m = new StorageObject();
581cb0ef41Sopenharmony_ci  bench.start();
591cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
601cb0ef41Sopenharmony_ci    m[`i${i}`] = i;
611cb0ef41Sopenharmony_ci    m[`s${i}`] = String(i);
621cb0ef41Sopenharmony_ci    assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
631cb0ef41Sopenharmony_ci    m[`i${i}`] = undefined;
641cb0ef41Sopenharmony_ci    m[`s${i}`] = undefined;
651cb0ef41Sopenharmony_ci  }
661cb0ef41Sopenharmony_ci  bench.end(n);
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cifunction fakeMap() {
701cb0ef41Sopenharmony_ci  const m = {};
711cb0ef41Sopenharmony_ci  return {
721cb0ef41Sopenharmony_ci    get(key) { return m[`$${key}`]; },
731cb0ef41Sopenharmony_ci    set(key, val) { m[`$${key}`] = val; },
741cb0ef41Sopenharmony_ci    get size() { return Object.keys(m).length; },
751cb0ef41Sopenharmony_ci    has(key) { return Object.hasOwn(m, `$${key}`); },
761cb0ef41Sopenharmony_ci  };
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_cifunction runFakeMap(n) {
801cb0ef41Sopenharmony_ci  const m = fakeMap();
811cb0ef41Sopenharmony_ci  bench.start();
821cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
831cb0ef41Sopenharmony_ci    m.set(`i${i}`, i);
841cb0ef41Sopenharmony_ci    m.set(`s${i}`, String(i));
851cb0ef41Sopenharmony_ci    assert.strictEqual(String(m.get(`i${i}`)), m.get(`s${i}`));
861cb0ef41Sopenharmony_ci    m.set(`i${i}`, undefined);
871cb0ef41Sopenharmony_ci    m.set(`s${i}`, undefined);
881cb0ef41Sopenharmony_ci  }
891cb0ef41Sopenharmony_ci  bench.end(n);
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_cifunction runMap(n) {
931cb0ef41Sopenharmony_ci  const m = new Map();
941cb0ef41Sopenharmony_ci  bench.start();
951cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
961cb0ef41Sopenharmony_ci    m.set(`i${i}`, i);
971cb0ef41Sopenharmony_ci    m.set(`s${i}`, String(i));
981cb0ef41Sopenharmony_ci    assert.strictEqual(String(m.get(`i${i}`)), m.get(`s${i}`));
991cb0ef41Sopenharmony_ci    m.set(`i${i}`, undefined);
1001cb0ef41Sopenharmony_ci    m.set(`s${i}`, undefined);
1011cb0ef41Sopenharmony_ci  }
1021cb0ef41Sopenharmony_ci  bench.end(n);
1031cb0ef41Sopenharmony_ci}
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_cifunction main({ n, method }) {
1061cb0ef41Sopenharmony_ci  switch (method) {
1071cb0ef41Sopenharmony_ci    case 'object':
1081cb0ef41Sopenharmony_ci      runObject(n);
1091cb0ef41Sopenharmony_ci      break;
1101cb0ef41Sopenharmony_ci    case 'nullProtoObject':
1111cb0ef41Sopenharmony_ci      runNullProtoObject(n);
1121cb0ef41Sopenharmony_ci      break;
1131cb0ef41Sopenharmony_ci    case 'nullProtoLiteralObject':
1141cb0ef41Sopenharmony_ci      runNullProtoLiteralObject(n);
1151cb0ef41Sopenharmony_ci      break;
1161cb0ef41Sopenharmony_ci    case 'storageObject':
1171cb0ef41Sopenharmony_ci      runStorageObject(n);
1181cb0ef41Sopenharmony_ci      break;
1191cb0ef41Sopenharmony_ci    case 'fakeMap':
1201cb0ef41Sopenharmony_ci      runFakeMap(n);
1211cb0ef41Sopenharmony_ci      break;
1221cb0ef41Sopenharmony_ci    case 'map':
1231cb0ef41Sopenharmony_ci      runMap(n);
1241cb0ef41Sopenharmony_ci      break;
1251cb0ef41Sopenharmony_ci    default:
1261cb0ef41Sopenharmony_ci      throw new Error(`Unexpected method "${method}"`);
1271cb0ef41Sopenharmony_ci  }
1281cb0ef41Sopenharmony_ci}
129