var SomeThing = (function () {
function SomeThing() {
this._value = 1;
}
SomeThing.prototype.setValue1 = function (v) {
this._value = v;
return this;
};
SomeThing.prototype.setValue2 = function (v) {
this._value = v;
};
return SomeThing;
}()),
st = new SomeThing();
st.setValue1(2)
.setValue1(2)
.setValue1(2)
.setValue1(2)
.setValue1(2)
.setValue1(2)
.setValue1(2)
.setValue1(2)
.setValue1(2)
.setValue1(2)
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
st.setValue2(2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
return chain | |
reref |
Test name | Executions per second |
---|---|
return chain | 8186460.0 Ops/sec |
reref | 808086.6 Ops/sec |
I'd be happy to help explain the benchmark.
Overview
The provided JSON represents two test cases for a JavaScript microbenchmark. The test cases are designed to measure the performance difference between two approaches: "return chain" and "ref again".
Return Chain Approach
In this approach, the setValue1
method is called with a return value of this
, which returns the instance itself. This allows for chaining multiple method calls together.
Example:
st.setValue1(2) // returns st
.setValue1(2) // returns st (again)
...
Ref Again Approach
In this approach, a local variable is used to store the return value of setValue1
, and then another call to setValue1
with the same variable is made.
Example:
st.setValue2(2) // assigns 2 to _value (local var)
st.setValue2(2) // assigns 2 to _value (again, different local var)
Benchmark Test Cases
There are two test cases:
setValue1
is called multiple times in a row.setValue1
, and then another call is made with that variable.Pros and Cons
Here are some pros and cons for each approach:
Library and Special JS Features
In this benchmark, no external libraries or special JavaScript features are used. The test cases only rely on standard JavaScript syntax.
Other Alternatives
If the developers want to explore other approaches, they can consider alternatives such as:
bind
or arrow functions
)Keep in mind that these alternatives may require additional testing and analysis to ensure that they provide the desired performance improvements.