Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String search vs Array search vs Obj key search
(version: 1)
Comparing performance of:
String search vs Array search vs Obj key search
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function strMonth(str) { const months3 = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|янв|фев|мар|апр|май|июн|июл|авг|сен|окт|ноя|дек"; return Math.floor(months3.indexOf(str.slice(0, 3).toLowerCase()) / 4) % 12 + 1 } function arrMonth(str) { const months3 = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec","янв","фев","мар","апр","май","июн","июл","авг","сен","окт","ноя","дек"]; return Math.floor(months3.indexOf(str.slice(0, 3).toLowerCase()) % 12 + 1); } function objMonth(str) { const months3 = { jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12,янв:1,фев:2,мар:3,апр:4,май:5,июн:6,июл:7,авг:8,сен:9,окт:10,ноя:11,дек:12 }; return months3[str.slice(0, 3).toLowerCase()]; }
Tests:
String search
strMonth("apr"); strMonth("dec"); strMonth("дек");
Array search
arrMonth("apr"); arrMonth("dec"); arrMonth("дек");
Obj key search
objMonth("apr"); objMonth("dec"); objMonth("дек");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String search
Array search
Obj key search
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String search
38484048.0 Ops/sec
Array search
1980257.4 Ops/sec
Obj key search
6117933.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition JSON** The benchmark is designed to compare three different methods for searching months in a string: 1. **String Search**: Using the `strMonth` function, which searches for the first 3 characters of the input string in an array of month names. 2. **Array Search**: Using the `arrMonth` function, which directly indexes into the `months3` array to find the matching month. 3. **Object Key Search**: Using the `objMonth` function, which uses an object to map month abbreviations to their corresponding numbers. **Pros and Cons of each approach** 1. **String Search (strMonth)**: * Pros: Simple to implement, can handle multiple month names in a single array. * Cons: May have slower performance due to the use of `indexOf` and string manipulation. 2. **Array Search (arrMonth)**: * Pros: Fastest execution time, as it directly indexes into the array using the input value as an index. * Cons: Requires a fixed-size array with all month names, which can be cumbersome to maintain. 3. **Object Key Search (objMonth)**: * Pros: Can handle multiple month names and is generally faster than string search due to object lookups. * Cons: May require more memory to store the large object. **Library Used** In this benchmark, no external libraries are explicitly mentioned or used. However, the `months3` array is assumed to be a predefined constant that contains all 12 month names in both uppercase and lowercase versions (e.g., "jan" and "янв"). **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's commonly available in modern browsers. It relies on basic functions like `indexOf`, `slice`, and object lookups. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Regular Expressions**: You could use regular expressions to match month names against a predefined pattern. 2. **Locale-specific month names**: Instead of using fixed-month-name arrays or objects, you might want to use locale-specific month names from the `Intl.DateTimeFormat` API. 3. **Using a data structure like Trie**: A Trie (prefix tree) data structure can be used to store and search for month names efficiently. Keep in mind that each alternative approach has its pros and cons, and may require additional considerations or modifications to fit your specific use case.
Related benchmarks:
toLocalDateString vs Array index
Generate Date In A Month
Array Apply vs Array Spread
Array Apply vs Array Spread vs Array Fill
Array splice is slow
Comments
Confirm delete:
Do you really want to delete benchmark?