Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.create
(version: 0)
Comparing performance of:
Plain object vs Person class instance vs Object.create()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
globalThis.create = Object.create globalThis.descriptor = { lastName: { writeable: true, enumerable: true, configurable: false, value: "" }, firstName: { writeable: true, enumerable: true, configurable: false, value: "" }, name: { writeable: true, enumerable: true, configurable: false, get () { return `${ this.firstName} ${ this.lastName }` } }, }; globalThis.Person = class Person { lastName = "" firstName = "" constructor (firstName, lastName) { this.lastName = lastName this.firstName = firstName } get name () { return `${ this.firstName} ${ this.lastName }` } } globalThis.plainObject = {} globalThis.personInstance = new Person globalThis.createdPerson = create(null, descriptor)
Tests:
Plain object
plainObject = { lastName: "Doe", firstName: "John", get name () { return `${ this.firstName} ${ this.lastName }` } }
Person class instance
personInstance = new Person("Doe", "John")
Object.create()
createdPerson = create(null, descriptor) createdPerson.lastName = "Doe" createdPerson.firstName = "John"
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Plain object
Person class instance
Object.create()
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark test cases and explain what's being tested, compared, and their pros and cons. **Benchmark Definition JSON** The benchmark definition JSON defines three different approaches to creating an object: 1. **Plain Object**: `plainObject = { lastName: "Doe", firstName: "John", get name () { return `${ this.firstName} ${ this.lastName }` } }` * This test creates a plain JavaScript object using the literal syntax `{ ... }`. The `name` property is defined as a getter function that returns a string concatenation. 2. **Person Class Instance**: `personInstance = new Person("Doe", "John")` * This test creates an instance of a `Person` class using the `new` keyword and passing arguments for the constructor. The `name` property is defined as a getter function that returns a string concatenation. 3. **Object.create()**: `createdPerson = create(null, descriptor)` * This test uses the `Object.create()` method to create an object from a given prototype (`null`) and descriptor (`descriptor`). The `name` property is also defined as a getter function that returns a string concatenation. **Options Compared** The benchmark compares the performance of these three approaches: 1. **Plain Object**: Uses literal syntax to create an object. 2. **Person Class Instance**: Uses a class definition and constructor to create an instance. 3. **Object.create()**: Uses the `Object.create()` method to create an object from a prototype. **Pros and Cons** Here's a brief summary of each approach: 1. **Plain Object**: * Pros: Simple, fast, and lightweight. No overhead from class definitions or constructor calls. * Cons: May not be suitable for complex data structures or inheritance scenarios. 2. **Person Class Instance**: * Pros: Supports inheritance and encapsulation using classes and constructors. Can be more readable for complex logic. * Cons: Has a slight performance overhead due to class definition and constructor calls. 3. **Object.create()**: * Pros: Allows for fine-grained control over object creation, including prototype chaining and descriptor configuration. * Cons: May have a higher overhead compared to plain objects or classes. **Library Usage** None of the benchmark test cases explicitly use any external libraries beyond the built-in JavaScript functionality. However, the `Person` class definition may implicitly rely on libraries like ES6 Classes or prototype-based inheritance. **Special JS Feature/Syntax** The benchmark uses several ES6+ features, including: 1. **Classes**: Defined using the `class` keyword. 2. **Object literals**: Used to define objects and prototypes. 3. **Arrow functions**: Not used in this benchmark, but could be relevant for other tests. 4. **Template literals**: Used to create string concatenation expressions. **Other Alternatives** If you need to test alternative approaches, here are some suggestions: 1. **Function constructors**: Instead of using `Object.create()`, you can use function constructors like `new Function(...)` to create objects. 2. **Proxy objects**: You could add another test case that uses proxy objects (`Proxy`) for object creation and manipulation. 3. **Inheritance patterns**: Other inheritance patterns, such as prototype chaining or closure-based inheritance, could be tested using different classes and constructors. Keep in mind that these alternatives might introduce additional complexity and variability in your benchmark tests.
Related benchmarks:
Object
Object and no-object speed
class private prop and read prop fix test5
Spread vs Assign bensilverm
Comments
Confirm delete:
Do you really want to delete benchmark?