Map speed test (v9)

Revision 9 of this benchmark created on


Setup

  class ExtendedArray extends Array {
  constructor() {
    super();
    this.map = new Map();
  }

  set(index, value) {
    this.map.set(index, value);
  }

  get(index) {
    return this.map.get(index);
  }
}

Object.defineProperty(ExtendedArray.prototype, 'value', {
  set(index, value) {
    this.set(index, value);
  },
  get() {
    return {
      [Symbol.toPrimitive](hint) {
        if (hint === 'number') {
          return this.get(this);
        }
      },
    };
  },
});
  
  const arraySize = 1000000;
  const testArray2 = new ExtendedArray();
  const map = new Map();
  

Test runner

Ready to run.

Testing in
TestOps/sec
No proxy
function testArrayReadWrite(loops) {
  // Perform the test for the specified number of loops
  for (let i = 0; i < loops; i++) {
    // Set an index to use for setting and reading the value
    const index = Math.floor(Math.random() * arraySize);

    // Set a value at the index
    map.set(index, 42)

    // Read the value at the index
    const value = map.get(index);
  }
}

// Call the test function with the desired number of loops
testArrayReadWrite(1000);
ready
Get/set proxy
function testArrayReadWrite(loops) {
  // Perform the test for the specified number of loops
  for (let i = 0; i < loops; i++) {
    // Set an index to use for setting and reading the value
    const index = Math.floor(Math.random() * arraySize);

    // Set a value at the index
    testArray2[index] = 42;

    // Read the value at the index
    const value = testArray2[index];
  }
}

// Call the test function with the desired number of loops
testArrayReadWrite(1000);
 
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.