Object creation/access

Benchmark created on


Setup

proto1 = { a: 1 }
    proto2 = Object.create(proto1, { b: {value: 2 }})
    proto3 = Object.create(proto2, { c: {value: 3 }})
    proto4 = Object.create(proto3, { d: {value: 4 }})
    
    // Constructor
    function Foo(x, y) {
      this.x = x
      this.y = y }
    
    Foo.prototype = Object.create(proto4)
    Foo.prototype.get = function() { return [this.x, this.y] }
    
    // Closured (flat)
    function extend(target, source) {
      for (var key in source)
        target[key] = source[key]
      return target }
    
    function make_foo(x, y) {
      // obviously a huge perf hit
      var instance = extend({}, proto4)
      instance.x = x
      instance.y = y
      instance.get = function(){ return [x, y] }
      return instance }
    
    // Cloning
    var foo = Object.create(proto4)
    foo.get = function() { return [this.x, this.y] }
    function create_foo(x, y) {
      var instance = Object.create(foo)
      instance.x = x
      instance.y = y
      return instance }
    
    // ad-hoc cloning
    function make(proto) {
      function Dummy(){}
      Dummy.prototype = proto
      return new Dummy }
    
    function make_adhoc_foo(x, y) {
      var instance = make(foo)
      instance.x = x
      instance.y = y
      return instance }
    
    // Changing the proto-chain
    function setproto_foo(x, y) {
      var instance = {}
      instance.__proto__ = foo
      instance.x = x
      instance.y = y
      return instance }
    
    // Instances
    var new_foo = new Foo(1, 1)
    var closure_foo = make_foo(1, 1)
    var cloned_foo = create_foo(1, 1)
    var adhoc_foo = make_adhoc_foo(1, 1)
    var slow_foo = setproto_foo(1, 1)
    var z = 0, x

Test runner

Ready to run.

Testing in
TestOps/sec
Creation (constructor)
x = new Foo(1, 1)
ready
Creation (closured)
x = make_foo(1, 1)
ready
Creation (cloning)
x = create_foo(1, 1)
ready
Creation (set proto-chain)
x = setproto_foo(1, 1)
ready
Slot access (constructor)
z += new_foo.x + new_foo.y
ready
Slot access (closured)
z += closure_foo.x + closure_foo.y
ready
Slot access (cloned)
z += cloned_foo.x + cloned_foo.y
ready
Slot access (set proto)
z += slow_foo.x + slow_foo.y
ready
Fallback slot access (constructor)
z += new_foo.a + new_foo.b + new_foo.c + new_foo.d
ready
Fallback slot access (closured)
// Doesn't apply as it doesn't really inherit anything =/
z += closure_foo.a + closure_foo.b + closure_foo.c + closure_foo.d
ready
Fallback slot access (cloned)
z += cloned_foo.a + cloned_foo.b + cloned_foo.c + cloned_foo.d
ready
Fallback slot access (set proto)
z += slow_foo.a + slow_foo.b + slow_foo.c + slow_foo.d
ready
Slot creation (constructor)
new_foo.z = 1
ready
Slot creation (closured)
closure_foo.z = 1
ready
Slot creation (cloned)
cloned_foo.z = 1
ready
Slot creation (set proto)
slow_foo.z = 1
ready
Call shared (constructor)
x = new_foo.get()
ready
Call shared (closured)
// Again, doesn't apply because it's not on the prototype
x = closure_foo.get()
ready
Call shared (cloned)
x = cloned_foo.get()
ready
Call shared (set proto)
x = slow_foo.get()
ready
Creation (adhoc)
x = make_adhoc_foo(1, 1)
ready
Slot access (adhoc)
z += adhoc_foo.x + adhoc_foo.y
ready
Fallback slot access (adhoc)
z += adhoc_foo.a + adhoc_foo.b + adhoc_foo.c + adhoc_foo.d
ready
Slot creation (adhoc)
adhoc_foo.z = 1
ready
Call shared (adhoc)
x = adhoc_foo.get()
ready

Revisions

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