<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Object.byString = function(o, s) {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
var a = s.split('.');
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i];
if (k in o) {
o = o[k];
} else {
return;
}
}
return o;
}
Object.byString({
a: 1
}, 'a');
Object.byString({
a: 1
}, 'a');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Case 1 | |
Case 2 |
Test name | Executions per second |
---|---|
Case 1 | 954271.6 Ops/sec |
Case 2 | 970146.6 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Definition
The benchmark definition is provided as a JSON object, which defines a JavaScript function called Object.byString
. This function takes two arguments: an object o
and a string s
. The function is designed to retrieve a value from the object using the provided string path. Here's a breakdown of what's being tested:
Object.byString
function is used to test how different browsers and platforms perform when accessing nested properties in an object.{a: 1}
, and the property path is 'a'
. However, as we'll see later, there are variations in this benchmark that test different paths.Options Compared
In this benchmark, two main options are being compared:
get()
method: Lodash is a popular JavaScript library that provides utility functions for common tasks. The get()
method is used to access nested properties in an object. By using Lodash, the benchmark ensures consistent behavior across different browsers and platforms.Object.byString
function provided by MeasureThat.net. This approach relies on the browser's built-in support for accessing nested properties.Pros and Cons
Here are some pros and cons of each approach:
get()
method:Library: Lodash
Lodash is a popular JavaScript library that provides a wide range of utility functions for common tasks. The get()
method is used to access nested properties in an object, which makes it an ideal choice for this benchmark.
In the provided JSON, you can see that the Html Preparation Code
includes a link to the Lodash CDN, ensuring that the library is available during testing.
Special JS Feature: ES6 Modules
The benchmark definition uses ES6 modules (import/export syntax) to define the Object.byString
function. This feature is supported in modern browsers and provides a more concise way of defining functions and variables.
While not all browsers support ES6 modules, MeasureThat.net likely relies on a subset of modern browsers that do. If you were to run this benchmark in an older browser that doesn't support ES6 modules, you might encounter issues with the code syntax.
Alternatives
If you're interested in exploring alternative approaches or testing other scenarios, here are some options:
Object.prototype.hasOwnProperty.call()
or in
operator.I hope this explanation helps you understand what's being tested in MeasureThat.net!