Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
class vs object
(version: 2)
Comparing performance of:
class vs object
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
class
const reportsData = Array(1000).map((_, index) => ({ user: { id: index, name: 'user' + index } })); class Report { constructor(report) { this.report = report; } reporterId() { this.report.user.id; } reporterName() { this.report.user.name; } } const classReports = reportsData.map(data => new Report(data)); classReports.forEach(report => console.log(report.reporterId, reporter.name))
object
const reportsData = Array(1000).map((_, index) => ({ user: { id: index, name: 'user' + index } })); const createReport = (report) => { return { ...report, get reporterId() { return this.user.id }, get reporterName() { return this.user.name }, } } const objectReports = reportsData.map(data => createReport(data)); objectReports.forEach(report => console.log(report.reporterId, reporter.name))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
class
object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
class
125958.2 Ops/sec
object
150189.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** MeasureThat.net is testing the performance difference between using classes and objects in JavaScript to create reports with a specific structure. The benchmark is designed to compare the execution speed of two approaches: 1. Using a class (`Report`) to create reports, as shown in the "class" test case. 2. Creating an object that acts like a report using a constructor function (`createReport`), as shown in the "object" test case. **Comparison of options** In this benchmark, we have two main options: 1. **Classes**: Using classes to create reports is a more traditional approach in JavaScript. Classes provide a way to organize code into namespaces and can be used to define constructors, methods, and properties. 2. **Objects with constructor functions**: This approach uses a constructor function (`createReport`) to create objects that mimic the behavior of a class. The object has properties and methods defined using the `this` keyword. **Pros and cons** **Classes:** Pros: * Easier to read and understand for developers familiar with object-oriented programming (OOP) concepts. * Provides more flexibility in defining constructors, methods, and properties. Cons: * May have overhead due to the creation of a new scope and the need to define a constructor function. * Less flexible than objects with constructor functions when it comes to creating ad-hoc objects. **Objects with constructor functions:** Pros: * Can be faster since there is no overhead from creating a new scope or defining a constructor function. * Provides more flexibility in creating ad-hoc objects without having to define an entire class. Cons: * May require more code to achieve the same result as using classes. * Less intuitive for developers unfamiliar with OOP concepts. **Library usage** There are no libraries used in these benchmark test cases. The code is self-contained and only uses built-in JavaScript features. **Special JS feature or syntax** There is no special JS feature or syntax being tested in this benchmark. It's a simple performance comparison of two approaches. **Alternatives** If you're looking for alternatives to MeasureThat.net, here are a few options: 1. **jsperf**: A popular online benchmarking tool that allows you to compare the execution speed of different JavaScript code snippets. 2. **Benchmark.js**: A lightweight and flexible benchmarking library for Node.js that provides a simple way to write benchmarks. 3. **Google Closure Compiler**: A compiler that optimizes JavaScript code for production, also includes a built-in benchmarking tool. These alternatives can help you create and run your own benchmarks, but MeasureThat.net is still a convenient option for comparing the performance of different JavaScript approaches.
Related benchmarks:
object.assign vs new class extends
ES6 Class vs Prototype vs Object Literal v2
Object creation: arrow function vs. class
Object creation: arrow function vs. class with methods
Comparison of classes vs prototypes vs object literals also including the inheritance
Comments
Confirm delete:
Do you really want to delete benchmark?