Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test property write access
(version: 2)
Comparing performance of:
2 - function vs 3 - method vs 4 - closure function vs 5 - es-5 setter vs 1 - direct access
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var testObj1 = {a: 28, b: 82, set_property(val) { this.property = val; }, set_property1(val) { this.property1 = val; }, set g_property(val) { this.property = val; }, set g_property1(val) { this.property1 = val; } , c: "hello", d: 983, e: 'lara', property: 32, f: '82828', property1: "asd"}; function set_property(obj, val) { obj["property"] = val; } function set_property1(obj, val) { obj["property1"] = val; } function set_property_cl(val) { testObj1["property"] = val; } function set_property1_cl(val) { testObj1["property1"] = val; } var C = 100000;
Tests:
2 - function
var result; for (var i = 0; i < C; i++) { set_property(testObj1, i); set_property1(testObj1, i+1); } result = testObj1["property"];
3 - method
var result; for (var i = 0; i < C; i++) { testObj1.set_property(i); testObj1.set_property1(i+1); } result = testObj1["property"];
4 - closure function
var result; for (var i = 0; i < C; i++) { set_property_cl(i); set_property1_cl(i+1); } result = testObj1["property"];
5 - es-5 setter
var result; for (var i = 0; i < C; i++) { testObj1.g_property = i; testObj1.g_property1 = i+1; } result = testObj1["property"];
1 - direct access
var result; for (var i = 0; i < C; i++) { testObj1.property = i; testObj1.property1 = i+1; } result = testObj1["property"];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
2 - function
3 - method
4 - closure function
5 - es-5 setter
1 - direct access
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark measures the performance of accessing and setting properties on an object in JavaScript. The test cases compare different approaches to achieve this: 1. **Direct Access**: `testObj1[\"property\"]` 2. **Method Call**: `testObj1.set_property(i)` 3. **Closure Function**: `set_property_cl(i)` and `set_property1_cl(i+1)` 4. **ES5 Setter**: `testObj1.g_property = i` **What's being tested** The benchmark measures the execution speed of each approach in setting and accessing properties on an object. The test cases use a large number of iterations (`C`) to ensure that the results are representative. **Options Compared** Here are the options compared: * **Direct Access**: uses the bracket notation `[]` to access and set properties. * **Method Call**: calls a custom method `set_property(i)` or `set_property1(i+1)` to set properties. * **Closure Function**: uses a closure function `set_property_cl(i)` and `set_property1_cl(i+1)` to set properties, which captures the iteration variable `i` in scope. **Pros and Cons** Here are some pros and cons for each approach: * **Direct Access**: + Pros: simple, efficient, widely supported. + Cons: may lead to errors due to typos or invalid property names. * **Method Call**: + Pros: decouples setting and accessing properties, can be used with multiple methods. + Cons: requires creating a custom method, may incur overhead of method call. * **Closure Function**: + Pros: captures iteration variable `i` in scope, can be useful for complex logic. + Cons: introduces additional complexity, requires understanding of closures. **Other Considerations** * The benchmark uses Firefox 58 as the test browser. Other browsers and versions may produce different results. * The `C` variable determines the number of iterations used to measure performance. Increasing this value can provide more accurate results but also increases execution time. * The use of ES5 Setter (`testObj1.g_property = i`) might not be supported by all browsers or older JavaScript engines. **Alternatives** Other approaches to setting and accessing properties on an object include: * Using a property accessor (e.g., `Object.defineProperty()` with getter and setter functions) * Using a library like Lodash's `set` function * Implementing a custom setter function using a technique like memoization or caching Note that these alternatives may introduce additional complexity or performance overhead compared to the options tested in this benchmark.
Related benchmarks:
test property access
test property read access
test property write access
Object spread
Comments
Confirm delete:
Do you really want to delete benchmark?