var Foo = function() {};
Foo.prototype._property = 'foo';
Foo.prototype.propertyAccess = function(param) {
var property = this._property;
this.noCache(param);
}
Foo.prototype.noCache = function(param) {
console.log(this._property + '' + param);
}
var Bar = function() {};
Bar.prototype._property = 'bar';
Bar.prototype.propertyAccess = function(param) {
var property = this._property;
this.cache(property, param);
}
Bar.prototype.cache = function(property, param) {
console.log(property + ' ' + param);
}
var foo = new Foo(),
i = 1000;
while(i--) {
foo.propertyAccess(i);
}
var bar = new Bar(),
i = 1000;
while(i--) {
bar.propertyAccess(i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Property access | |
Cached value |
Test name | Executions per second |
---|---|
Property access | 61.5 Ops/sec |
Cached value | 62.4 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmark that tests the performance difference between two approaches: Prototypal property access and Passing cached value.
In this benchmark, we have two classes: Foo
and Bar
. Both classes have a private property _property
initialized with a string value ('foo'
for Foo
and 'bar'
for Bar
). They also have a public method propertyAccess
which is used to access these properties.
The key difference between the two approaches lies in how the propertyAccess
method accesses the _property
. For Prototypal property access, the method directly accesses the private property using this._property
. In contrast, for Passing cached value, the method passes the cached value of _property
as an argument to another method noCache
, which logs the value and a parameter.
Comparison Options
There are two comparison options:
_property
._property
as an argument to noCache
.Pros and Cons
Library and Special JS Features
There is no explicit library used in this benchmark. However, it's worth noting that some JavaScript engines (like V8) use a technique called "cache thrashing" when dealing with private properties accessed through multiple methods. This can affect performance in certain scenarios.
Other Alternatives
For similar benchmarks, you might consider testing other approaches such as: