proxy-profiling

JavaScript performance comparison

Test case created by whizzzkid

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  class A {
      constructor() {
          this.foo = 'foo';
      }
      
      bar() {
          return this.foo;
      }
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Wrapper
let a = new A();
let d = {
    bar: () => 'decorated' + a.bar()
}
if (d.bar() !== 'decoratedfoo') {
    throw new Error('broken');
}
pending…
Proxies
let a = new A();

let d = new Proxy(a, {
    get: (target, property) => () => 'decorated' + target[property]()
})
if (d.bar() !== 'decoratedfoo') {
    throw new Error('broken');
}
pending…
Monkey Patching
let a = new A();

const patch = (target, patches) => (prop, ...args) => {
    if (prop in patches) {
        const value = patches[prop];
        if (typeof value === 'function') {
          return value(target, ...args);
        }
        return value;
    }
    return target(...args);
};

let d = patch(a, {
  bar: (target) => 'decorated' + target.bar()
});

if (d('bar') !== 'decoratedfoo') {
    throw new Error('broken');
}
pending…
Prototypical
let a = new A();

const decorator = (target, decorations) => {
    const decorated = Object.create(target);
    for (let [key, value] of Object.entries(decorations)) {
        decorated[key] = (...args) => value(target, ...args);
    }
    return decorated;
}

let d = decorator(a, {
    bar: (target) => 'decorated' + target.bar()
});

if (d.bar() !== 'decoratedfoo') {
    throw new Error('broken');
}
pending…

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

0 Comments