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))
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))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
class | |
object |
Test name | Executions per second |
---|---|
class | 125958.2 Ops/sec |
object | 150189.4 Ops/sec |
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:
Report
) to create reports, as shown in the "class" test case.createReport
), as shown in the "object" test case.Comparison of options
In this benchmark, we have two main options:
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:
Cons:
Objects with constructor functions:
Pros:
Cons:
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:
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.