Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function call vs Object
(version: 0)
Compare Function calls vs Object
Comparing performance of:
object with regexp and replace vs function call
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myObject = { "/path1/pathA": "firstOne", "/path2/pathB": "secondOne", "/path3/pathC": "thirdOne" } var getMyObject = (path) => { if (path.includes("/path1/pathA")) { return "firstOne" } else if (path.includes("/path2/pathB")) { return "secondOne" } else if (path.includes("/path3/pathC")) { return "thirdOne" } }
Tests:
object with regexp and replace
const path = "/path1/pathA/pathX" const updated = path.replace(/pathX/i, '') const res = myObject[updated]; const path2 = "/path2/pathB/pathY" const updated2 = path2.replace(/pathY/i, '') const res2 = myObject[updated2]; const path3 = "/path3/pathC/pathZ" const updated3 = path3.replace(/pathZ/i, '') const res3 = myObject[updated3];
function call
const path = "/path1/pathA/pathX" const res = getMyObject(path); const path2 = "/path2/pathB/pathY" const res2 = getMyObject(path); const path3 = "/path3/pathC/pathZ" const res3 = getMyObject(path);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object with regexp and replace
function call
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Android 13; Mobile; rv:133.0) Gecko/133.0 Firefox/133.0
Browser/OS:
Firefox Mobile 133 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object with regexp and replace
2273216.0 Ops/sec
function call
439169184.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition:** The benchmark measures the performance of two approaches: 1. **Function call:** The `getMyObject` function is called with different paths as arguments, which returns a value based on a predefined condition. 2. **Object lookup with regular expression and replacement:** An object (`myObject`) is used to store values associated with specific paths. These paths are modified using regular expressions and replaced with an empty string, then looked up in the `myObject` object. **Pros and Cons:** 1. **Function call:** * Pros: + Easier to understand and maintain. + Can be optimized for performance through caching or memoization. * Cons: + May incur overhead due to function calls and returns. 2. **Object lookup with regular expression and replacement:** * Pros: + Can take advantage of the object's data structure and caching. * Cons: + May be slower due to regular expression parsing and string manipulation. **Library/Function used:** The `getMyObject` function uses a JavaScript regular expression (`i` flag) for pattern matching. The `replace()` method is also used with the `i` flag to replace parts of the path. **Special JS Feature/Syntax:** * None mentioned in the provided benchmark definition. **Alternative Approaches:** Other approaches that could be considered: 1. **Array lookup:** Instead of using an object, an array could be used to store values associated with specific indices. 2. **Caching:** Both approaches could benefit from caching to improve performance by reducing the number of times the function or object lookup is performed. 3. **Memoization:** The `getMyObject` function could be optimized using memoization techniques to cache and reuse previously computed results. The benchmark result shows that the Firefox 115 browser performs better for the **function call** approach (ExecutionsPerSecond: 10578787.0) compared to the **object with regexp and replace** approach (ExecutionsPerSecond: 3183395.5). This suggests that the function call approach is likely more efficient in this specific implementation.
Related benchmarks:
undefined vs. hasOwnProperty2
undefined vs. typeof vs. in vs. hasOwnProperty vs. Optional chaining
object property lookup: in operator vs undefined comparison
undefined vs evaluation vs hasOwnProperty
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?