window.AClass = class {
constructor() {
this.c = () => 5;
}
b() {
return 4;
}
}
window.AFunc = function() {
function c() {
return 5;
}
return {
b() {
return 4;
},
c,
};
};
const a = new AClass();
a.b();
a.c();
const a = new AFunc();
a.b();
a.c();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Class instance method lookup | |
Function-created object method lookup |
Test name | Executions per second |
---|---|
Class instance method lookup | 3718290.8 Ops/sec |
Function-created object method lookup | 3443502.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two approaches to invoke methods on objects:
AClass
) to access its methods.AFunc
) to access its methods.Options Compared
The benchmark compares the performance of these two approaches on:
AClass
with two methods: b()
and c()
.AFunc
that returns an object with two methods: b()
and c()
.Pros and Cons
Pros:
Cons:
Pros:
Cons:
Other Considerations
The benchmark also considers the following factors:
These factors can affect the performance of the benchmark, as different devices and browsers may have varying levels of optimization or caching.
Library Usage
In this case, the library used is not explicitly stated. However, it's likely that the class
keyword and object literals are part of the ECMAScript standard, which provides a way to define classes and objects in JavaScript.
Special JS Features/Syntax
This benchmark does not use any special JavaScript features or syntax beyond what's commonly supported by modern browsers.
Alternatives
If you want to create your own microbenchmarks using MeasureThat.net, here are some alternative approaches you could consider:
Map
).Keep in mind that the specific approach and testing scenario you choose will depend on your goals, expertise, and resources.