Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set num vs set fnCall vs typecheck then set
(version: 0)
Comparing performance of:
setNumber vs setCall vs setTypecheck(num) vs setTypecheck(call)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a:1 } function setNumber(key, value) { obj[key] = value; } function setCall(key, getValue) { obj[key] = getValue(obj); } function setTypecheck(key, value) { obj[key] = typeof value === "number" ? value : value(obj); } function getValue() { return 100; }
Tests:
setNumber
setNumber("a", 100);
setCall
setCall("a", getValue);
setTypecheck(num)
setTypecheck("a", 100);
setTypecheck(call)
setTypecheck("a", getValue);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
setNumber
setCall
setTypecheck(num)
setTypecheck(call)
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 provided benchmark JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition:** The benchmark definition is a JSON object that describes the experiment: ```json { "Name": "set num vs set fnCall vs typecheck then set", "Description": null, "Script Preparation Code": ..., "Html Preparation Code": null } ``` This benchmark involves three different approaches to set values on an object `obj`: 1. **setNumber**: sets the value using a direct assignment (`obj[key] = value;`) 2. **setCall**: uses a function call to set the value (`obj[key] = getValue(obj);`) 3. **setTypecheck**: first checks if the value is a number using `typeof value === "number"`, and then sets the value accordingly (`obj[key] = typeof value === "number" ? value : value(obj);`) **Individual Test Cases:** The benchmark consists of four test cases, each with its own `Benchmark Definition` string: ```json [ { "Benchmark Definition": "setNumber(\"a\", 100);", "Test Name": "setNumber" }, { "Benchmark Definition": "setCall(\"a\", getValue);", "Test Name": "setCall" }, { "Benchmark Definition": "setTypecheck(\"a\", 100);", "Test Name": "setTypecheck(num)" }, { "Benchmark Definition": "setTypecheck(\"a\", getValue);", "Test Name": "setTypecheck(call)" } ] ``` Each test case measures the execution time of its corresponding benchmark definition. **Library:** The `getValue` function is a custom library, not part of the standard JavaScript. Its purpose is to return a constant value (`return 100;`) for use in the `setCall` and `setTypecheck` functions. **Special JS Feature/Syntax:** There are no special features or syntax mentioned in this benchmark. **Pros and Cons of Different Approaches:** 1. **setNumber**: direct assignment is likely to be the fastest, as it avoids function calls and type checks. * Pros: simple, fast * Cons: may not handle non-numeric values correctly 2. **setCall**: using a function call introduces overhead, but provides flexibility for handling different types of values. * Pros: flexible, handles non-numeric values correctly * Cons: slower due to function calls and type checks 3. **setTypecheck**: first checks if the value is a number before setting it. This approach balances speed and safety. * Pros: balances speed and safety, handles non-numeric values correctly * Cons: introduces additional overhead from type checks **Other Alternatives:** If not using `getValue`, alternative implementations for the `setCall` function could use other constant values or functions to simulate different scenarios. For example, instead of `getValue() { return 100; }`, you could use: ```javascript function setConstantValue(key, value) { obj[key] = value; } ``` This would remove the overhead of a function call and directly assign the value.
Related benchmarks:
JavaScript spread operator vs Object.assign performance 22
JavaScript spread operator vs Object.assign performance 224
JavaScript spread operator vs Object.assign performance 22476
lodash.mapKeys vs Native
ES6 property (get/set) & getter/setter function vs direct access vs closure
Comments
Confirm delete:
Do you really want to delete benchmark?