<html>
<head></head>
<body>
var doc = new Document({width: 50, height: 50});
</body>
</html>
var Document = function(Config) {
this.config = {
width: "auto",
height: "auto"
};
this.init(Config);
};
Document.prototype.init = function(Config) {
if(typeof Config == "object") {
for(prop in Config) {
if(this.config.hasOwnProperty(prop))
this.config[prop] = Config[prop];
}
}
};
Document.prototype.init = function(Config) {
var keys = Object.keys(Config);
for(var i = 0; i < keys.length; i++)
if(this.config.hasOwnProperty(keys[i])) this.config[keys[i]] = Config[keys[i]];
};
Document.prototype.init = function(Config) {
var keys = Object.keys(Config);
for(var i = 0; i < keys.length; i++)
if(this.config[keys[i]]) this.config[keys[i]] = Config[keys[i]];
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
classic | |
new1 | |
new2 |
Test name | Executions per second |
---|---|
classic | 7705120.0 Ops/sec |
new1 | 7804041.0 Ops/sec |
new2 | 6630885.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The provided benchmark measures the performance difference between three approaches for initializing an object in a Document
class. The class has a config
property, and the initialization method (init
) is used to set this property based on a configuration object passed as an argument.
Approaches Compared
There are two main approaches compared:
for...in
loop to iterate over the properties of the configuration object and update the config
object.Object.keys()
method to get an array of the property names in the configuration object, and then iterates over this array using a for...loop
.Object.keys()
, but with a slight optimization by checking if each property is already present in the config
object before updating it.Pros and Cons of Each Approach
for...in
.for...in
. Cons - may not work as expected in some older browsers that don't support Object.keys()
.Library and Special Features
There is no explicit library mentioned in this benchmark, but it's likely that MeasureThat.net uses a JavaScript runtime environment like V8 (used by Google Chrome) or SpiderMonkey (used by Mozilla Firefox).
No special JavaScript features or syntax are used in this benchmark. The code is written in standard ECMAScript.
Alternatives
For creating microbenchmarks and comparing performance, you can consider the following alternatives:
By using one of these alternatives, you can create and run your own JavaScript microbenchmarks on MeasureThat.net or other platforms, comparing the performance of different approaches and libraries.