Script Preparation code:
x
 
class CustomWeakMap {
  constructor(relationKey = '__relation') {
    this.relationKey = relationKey;
  }
  // 设置键值对 - 使用直接属性赋值以提高性能
  set(key, value) {
    key[this.relationKey] = value;
    return this;
  }
  // 获取键对应的值
  get(key) {
    return key[this.relationKey];
  }
  // 检查键是否存在
  has(key) {
    return this.relationKey in key;
  }
  // 删除键值对
  delete(key) {
    return delete key[this.relationKey];
  }
}
var to0 = {};
var to1 = {};
var to2 = {}
var wm = new WeakMap();
var sy = Symbol();
var cy = new CustomWeakMap();
wm.set(to1, 1);
to1[sy] = 1;
cy.set(to2,1)
Tests:
  • WeakMap set

     
    wm.set(to0, 1);
  • Symbol Property set

     
    to0[sy] = 1;
  • WeakMap get

     
    let r = wm.get(to1);
  • Symbol Property get

     
    let r = to1[sy];
  • CustomWeakMap set

     
    cy.set(to2, 1);
  • CustomWeakMap get

     
    let r = cy.get(to2);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    WeakMap set
    Symbol Property set
    WeakMap get
    Symbol Property get
    CustomWeakMap set
    CustomWeakMap get

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 21 days ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
WeakMap set 102614104.0 Ops/sec
Symbol Property set 173704560.0 Ops/sec
WeakMap get 97679648.0 Ops/sec
Symbol Property get 153358800.0 Ops/sec
CustomWeakMap set 167226688.0 Ops/sec
CustomWeakMap get 173829632.0 Ops/sec