Test name | Executions per second |
---|---|
Constructor parameters | 167.4 Ops/sec |
Constructor object | 50.8 Ops/sec |
Factory function with object | 131.5 Ops/sec |
Factory function with definition and parameters | 149.1 Ops/sec |
<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
class Component {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
for(let i = 0; i < 100000; i++) {
const component = new Component(i, i);
}
class Component {
constructor(params) {
const keys = Object.keys(params);
for(let i = 0, l = keys.length; i < l; i++) {
this[keys[i]] = params[i];
}
}
}
for(let i = 0; i < 100000; i++) {
const component = new Component({ x: i, y: i});
}
const createComponent = (params) => {
const component = {};
const keys = Object.keys(params);
for(let i = 0, l = keys.length; i < l; i++) {
component[keys[i]] = params[i];
}
return component;
}
for(let i = 0; i < 100000; i++) {
const component = createComponent({ x: i, y: i});
}
const positionComponentDef = {
x: Number,
y: Number,
};
const createComponent= (def, params) => {
const component = {};
const keys = Object.keys(def);
for(let i = 0, l = keys.length; i < l; i++) {
component[keys[i]] = params[i];
}
return component;
}
for(let i = 0; i < 100000; i++) {
const component = createComponent(positionComponentDef, i, i);
}