<html>
<head></head>
<body>
<script type="text/javascript">
var doc = new Document({width: 50, height: 50});
</script>
</body>
</html>
var Document = function(Config) {
this.config = {
width: "auto",
height: "auto"
};
this.init(Config);
};
Document.prototype.init = function(Config) {
var config = this.config;
if(typeof Config == "object") {
for(prop in Config) {
if(config.hasOwnProperty(prop))
this.config[prop] = Config[prop];
}
}
};
Document.prototype.init = function(Config) {
var config = this.config;
if(typeof Config == "object") {
for(var prop in Config) {
if(config.hasOwnProperty(prop))
this.config[prop] = Config[prop];
}
}
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
prop not declared | |
prop declared |
Test name | Executions per second |
---|---|
prop not declared | 8284570.5 Ops/sec |
prop declared | 3809781.8 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of JavaScript objects in iterating over properties, specifically focusing on whether the property is declared or not.
Options Compared
There are two options being compared:
var prop in Config
(Test Case: "prop not declared")var prop in Config
with a declaration (let
or const
, assuming they're equivalent to var
) - however, this is not the exact option being tested as declared variables are skipped in ECMAScript 6. Hence it's more like var
keyword only which in modern browsers behave same way as let
.hasOwnProperty()
: Both tests use this method.Pros and Cons
in
operator (both variants):hasOwnProperty()
:Library and Purpose
No external libraries are used in this benchmark. The Document
class is a custom class created for this benchmark, with an init()
method that demonstrates the iteration over properties using both options.
Special JS Feature or Syntax
The benchmark does not utilize any special JavaScript features or syntax beyond what's currently supported in modern browsers (ECMAScript 6 and later).
Other Alternatives
If you wanted to explore other approaches for iterating over object properties, consider:
for...in
loop: Similar to using the in
operator, but can be more readable.Object.keys()
and a forEach
loop: A more modern approach, utilizing the keys
method of objects.Map
or Set
data structures: Depending on your use case, these may offer better performance for iterating over properties.Keep in mind that benchmarking performance can be complex, and results may vary depending on browser implementation, hardware, and other factors.