Skip to content

Instantly share code, notes, and snippets.

@juliarose
Created June 30, 2024 12:00
Show Gist options
  • Save juliarose/833e8ca41908d2614532dd4a0d7ed346 to your computer and use it in GitHub Desktop.
Save juliarose/833e8ca41908d2614532dd4a0d7ed346 to your computer and use it in GitHub Desktop.
function createBencher(iters) {
return function runBench(label, fn) {
console.time(label);
for (let i = 0; i < iters; i++) {
fn();
}
console.timeEnd(label);
}
}
class Assigner {
constructor(props) {
Object.assign(this, props);
}
}
class AssignerForIn {
constructor(props) {
for (let key in props) {
this[key] = props[key];
}
}
}
class DirectAssigner_4props {
constructor(props) {
this.name = props.name;
this.color = props.color;
this.age = props.age;
this.city = props.city;
}
}
class DirectAssigner_8props {
constructor(props) {
this.name = props.name;
this.color = props.color;
this.age = props.age;
this.city = props.city;
this.favorite_color = props.favorite_color;
this.favorite_food = props.favorite_food;
this.favorite_animal = props.favorite_animal;
this.favorite_sport = props.favorite_sport;
}
}
class DirectAssigner_12props {
constructor(props) {
this.name = props.name;
this.color = props.color;
this.age = props.age;
this.city = props.city;
this.favorite_color = props.favorite_color;
this.favorite_food = props.favorite_food;
this.favorite_animal = props.favorite_animal;
this.favorite_sport = props.favorite_sport;
this.birth_date = props.birth_date;
this.birth_country = props.birth_country;
this.birth_state = props.birth_state;
this.birth_city = props.birth_city;
}
}
const obj_4props = {
name: 'John',
color: 'blue',
age: 25,
city: 'New York',
};
const obj_8props = {
name: 'John',
color: 'blue',
age: 25,
city: 'New York',
favorite_color: 'red',
favorite_food: 'pizza',
favorite_animal: 'dog',
favorite_sport: 'soccer',
};
const obj_12props = {
name: 'John',
color: 'blue',
age: 25,
city: 'New York',
favorite_color: 'red',
favorite_food: 'pizza',
favorite_animal: 'dog',
favorite_sport: 'soccer',
birth_date: '01/01/1995',
birth_country: 'USA',
birth_state: 'California',
birth_city: 'Los Angeles',
};
const runBench = createBencher(1e6);
runBench('Object.assign with 4 properties', () => new Assigner(obj_4props));
runBench('Object.assign with 8 properties', () => new Assigner(obj_8props));
runBench('Object.assign with 12 properties', () => new Assigner(obj_12props));
runBench('For in with 4 properties', () => new AssignerForIn(obj_4props));
runBench('For in with 8 properties', () => new AssignerForIn(obj_8props));
runBench('For in with 12 properties', () => new AssignerForIn(obj_12props));
runBench('Direct assignment with 4 properties', () => new DirectAssigner_4props(obj_4props));
runBench('Direct assignment with 8 properties', () => new DirectAssigner_8props(obj_8props));
runBench('Direct assignment with 12 properties', () => new DirectAssigner_12props(obj_12props));
// Object.assign with 4 properties: 46.676ms
// Object.assign with 8 properties: 93.73ms
// Object.assign with 12 properties: 148.207ms
// For in with 4 properties: 52.75ms
// For in with 8 properties: 115.907ms
// For in with 12 properties: 184.789ms
// Direct assignment with 4 properties: 7.567ms
// Direct assignment with 8 properties: 12.467ms
// Direct assignment with 12 properties: 11.392ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment