Object Creation
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
let myClassObject;
let myFuncObject1;
let myFuncObject2;
// using the `class` keyword
class MyClassObject {
constructor(initVal) {
this.myVal = initVal;
}
set(x) {
this.myVal = x;
}
}
// using a function with `this`
const makeObject1 = initVal => {
return {
myVal: initVal,
set: function(x) {
this.myVal = x;
},
};
};
// using a function without `this`
const makeObject2 = initVal => {
let myVal = initVal;
return {
get: function() {
return myVal;
},
set: function(val) {
myVal = val;
},
};
};
};
Benchmark.prototype.teardown = function() {
delete myClassObject;
delete myFuncObject1;
delete myFuncObject2;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
ES2015 Classes
|
|
pending… |
Function with
this
|
|
pending… |
Function without
this
|
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
0 Comments