Array proxy to Map methods (random access) (v11)

Revision 11 of this benchmark created on


Setup

  // Create an array with 1 million elements
  const arraySize = 1000000;
  const testArray = new Array(arraySize);
  
  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);
        }
      },
    };
  },
});
  
   // Create an array with 1 million elements
  const testArray2 = new ExtendedArray(arraySize);
  
  
 class ExtendedArray2 {
  constructor() {
    this.map = new Map();
    return new Proxy(this, {
      set: (target, prop, value) => {
        if (!isNaN(prop)) {
          target.map.set(Number(prop), value);
          return true;
        }
        target[prop] = value;
        return true;
      },
      get: (target, prop) => {
        if (!isNaN(prop)) {
          return target.map.get(Number(prop));
        }
        return target[prop];
      }
    });
  }
}

  const testArray3 = new ExtendedArray2(arraySize);
  
  const map = new Map();
  
const objectArray = new Array(arraySize)

class ObjectTest {
  index = 0;

  set value(value) {
    objectArray[this.index] = value;
  }

  get value() {
    return objectArray[this.index];
  }
}

const objectTest = new ObjectTest()

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
    testArray[index] = 42;

    // Read the value at the index
    const value = testArray[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
ES6 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
    testArray3[index] = 42;

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

// Call the test function with the desired number of loops
testArrayReadWrite(1000); 
ready
Plain map
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
Object 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
    objectTest.index = Math.floor(Math.random() * arraySize);

    // Set a value at the index
    objectTest.value = 42

    // Read the value at the index
    const value = objectTest.value;
  }
}

// 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.