setPrototypeOf vs assign

Benchmark created on


Setup

function obj () {
  	return {
  		a: 1,
  		b: 2,
  		c: 3,
  		z: 0
  	}
  }
  
  class Test {
  	constructor (obj) {
  		Object.assign(this, obj);
  	}
  	action() {
  		this.z = this.a + this.b + this.c; 
  	}
  }
  
  class Test1 {
  	constructor (obj) {
  		this.obj = obj;
  	}
  	action() {
      let obj = this.obj;
  		obj.z = obj.a + obj.b + obj.c; 
  	}
  }

Test runner

Ready to run.

Testing in
TestOps/sec
setPrototypeOf
Object.setPrototypeOf(obj(), Test.prototype).action();
ready
assign
new Test(obj()).action();
ready
internal
new Test1(obj()).action();
ready
proto
obj.__proto__ = Test.prototype;
obj.action();
ready

Revisions

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