Object vs. Seal vs. Freeze

JavaScript performance comparison

Revision 2 of this test case created by David Bruant

Info

I was wondering why my tests took much more time than average.

Seal was adding more security… but more latency too.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function thrower(){
        throw new Error('nope');
    }
    var frozenHandler = {
        set: thrower,
        defineProperty: thrower,
        delete: thrower
    };
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Regular Object Constructor
new(function Dummy(args) {
  this.foo = 'wont be foo';
  this.emptiness = [];
  this.mind_changer = false;
})({
  foo: 'will be foo',
  mind_changer: true
})
pending…
Sealing Object Constructor
new(function Dummy(args) {
  this.foo = 'wont be foo';
  this.emptiness = [];
  this.mind_changer = false;

  Object.seal(this);
})({
  foo: 'will be foo',
  mind_changer: true
})
pending…
Freezing Object Constructor
new(function Dummy(args) {
  this.foo = 'wont be foo';
  this.emptiness = [];
  this.mind_changer = false;

  Object.freeze(this);
})({
  foo: 'will be foo',
  mind_changer: true
})
pending…
Proxy-based freezing
new(function Dummy(args) {
  this.foo = 'wont be foo';
  this.emptiness = [];
  this.mind_changer = false;

  return new Proxy(this, frozenHandler);
})({
  foo: 'will be foo',
  mind_changer: true
})
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment