Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object camelCase conversion test
(version: 5)
Comparing performance of:
Objects vs Objects with cache vs Arrays vs Classes from arrays
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function snakeToCamel(str) { return str.replace(/(?!^)_(.)/g, (_, char) => char.toUpperCase()); } function objKeysToCamelCase(obj) { return Object.fromEntries(Object.entries(obj).map(([key, value]) => [snakeToCamel(key), value])) } const cache = new Map(); function objKeysToCamelCaseCached(obj) { const entries = Object.entries(obj).map(([key, value]) => { const cachedKey = cache.get(key); if (cachedKey) { newKey = cachedKey; } else { newKey = snakeToCamel(key); cache.set(key, newKey); } return [newKey, value]; }); return Object.fromEntries(entries); } const columns = ["firstName", "lastName", "jobTitle", "jobType", "sex", "ip"]; function createObjFromColumns(columns, values) { return Object.fromEntries(columns.map((column, index) => [column, values[index]])) } class User { constructor(firstName, lastName, jobTitle, jobType, sex, ip) { this.firstName = firstName; this.lastName = lastName; this.jobTitle = jobTitle; this.jobType = jobType; this.sex = sex; this.ip = ip; } } function createObjFromValues(values) { return new User(...values) } const dataObj = [{ "first_name": "Pedro", "last_name": "Heaney", "job_title": "Senior Intranet Officer", "job_type": "Specialist", "sex": "female", "ip": "183.166.63.121" }, { "first_name": "Domingo", "last_name": "Morissette", "job_title": "Regional Solutions Liaison", "job_type": "Executive", "sex": "female", "ip": "74.138.114.186" }, { "first_name": "Jammie", "last_name": "Steuber", "job_title": "Human Quality Coordinator", "job_type": "Agent", "sex": "female", "ip": "246.201.18.70" }, { "first_name": "Beverly", "last_name": "Price", "job_title": "Legacy Creative Coordinator", "job_type": "Producer", "sex": "female", "ip": "118.47.49.154" }, { "first_name": "Kathryn", "last_name": "Hayes-Sanford", "job_title": "Product Tactics Supervisor", "job_type": "Consultant", "sex": "male", "ip": "122.157.72.181" }, { "first_name": "Caterina", "last_name": "Walter", "job_title": "Senior Integration Manager", "job_type": "Assistant", "sex": "female", "ip": "118.193.86.90" }, { "first_name": "Beaulah", "last_name": "Hirthe", "job_title": "Corporate Group Agent", "job_type": "Supervisor", "sex": "female", "ip": "141.95.14.30" }, { "first_name": "Jennyfer", "last_name": "Schamberger", "job_title": "Internal Research Supervisor", "job_type": "Designer", "sex": "female", "ip": "125.191.241.202" }, { "first_name": "Dashawn", "last_name": "Lubowitz", "job_title": "Internal Directives Coordinator", "job_type": "Developer", "sex": "female", "ip": "13.4.43.29" }, { "first_name": "Deven", "last_name": "Franecki", "job_title": "Investor Tactics Analyst", "job_type": "Analyst", "sex": "male", "ip": "80.135.70.239" }, { "first_name": "Verna", "last_name": "Witting", "job_title": "National Identity Administrator", "job_type": "Strategist", "sex": "male", "ip": "89.31.73.192" }, { "first_name": "Beverly", "last_name": "Zieme", "job_title": "Internal Assurance Liaison", "job_type": "Director", "sex": "female", "ip": "44.223.7.248" }, { "first_name": "Lynn", "last_name": "Legros", "job_title": "Senior Research Architect", "job_type": "Liaison", "sex": "female", "ip": "210.59.97.220" }, { "first_name": "Roscoe", "last_name": "Breitenberg", "job_title": "Lead Usability Facilitator", "job_type": "Technician", "sex": "male", "ip": "251.191.56.168" }, { "first_name": "Janessa", "last_name": "Mann", "job_title": "Customer Response Executive", "job_type": "Administrator", "sex": "male", "ip": "249.114.54.13" }, { "first_name": "Gabrielle", "last_name": "Gerlach", "job_title": "District Operations Producer", "job_type": "Liaison", "sex": "female", "ip": "135.222.197.158" }, { "first_name": "Amina", "last_name": "Farrell", "job_title": "Direct Tactics Director", "job_type": "Agent", "sex": "male", "ip": "25.249.119.95" }, { "first_name": "Kelly", "last_name": "Langosh", "job_title": "Product Factors Consultant", "job_type": "Director", "sex": "female", "ip": "248.137.14.38" }, { "first_name": "Darrin", "last_name": "Feeney", "job_title": "Central Intranet Coordinator", "job_type": "Producer", "sex": "female", "ip": "122.29.87.2" }, { "first_name": "Durward", "last_name": "Schowalter", "job_title": "Human Accounts Facilitator", "job_type": "Supervisor", "sex": "male", "ip": "24.240.169.7" }, { "first_name": "Junior", "last_name": "Rice", "job_title": "Direct Factors Administrator", "job_type": "Agent", "sex": "female", "ip": "228.55.135.81" }, { "first_name": "Triston", "last_name": "Koepp", "job_title": "Investor Intranet Associate", "job_type": "Architect", "sex": "female", "ip": "130.34.36.173" }, { "first_name": "Hilario", "last_name": "Jacobi", "job_title": "District Response Analyst", "job_type": "Assistant", "sex": "female", "ip": "222.209.166.146" }, { "first_name": "Aiden", "last_name": "Rowe", "job_title": "Dynamic Web Specialist", "job_type": "Designer", "sex": "male", "ip": "130.117.131.39" }, { "first_name": "Titus", "last_name": "Vandervort", "job_title": "International Interactions Liaison", "job_type": "Facilitator", "sex": "female", "ip": "142.241.239.23" }, { "first_name": "Kole", "last_name": "Douglas", "job_title": "Corporate Functionality Officer", "job_type": "Assistant", "sex": "male", "ip": "0.251.249.142" }, { "first_name": "Oral", "last_name": "Durgan", "job_title": "Legacy Factors Associate", "job_type": "Technician", "sex": "male", "ip": "95.9.35.12" }, { "first_name": "Marilou", "last_name": "Parisian", "job_title": "Principal Configuration Producer", "job_type": "Specialist", "sex": "male", "ip": "103.48.32.68" }, { "first_name": "Marcella", "last_name": "Reynolds", "job_title": "Legacy Optimization Coordinator", "job_type": "Analyst", "sex": "male", "ip": "211.224.113.10" }, { "first_name": "Kayla", "last_name": "Wehner", "job_title": "Principal Intranet Supervisor", "job_type": "Technician", "sex": "male", "ip": "103.106.36.197" }, { "first_name": "Albina", "last_name": "Lakin-Mohr", "job_title": "National Branding Officer", "job_type": "Orchestrator", "sex": "male", "ip": "118.192.12.224" }, { "first_name": "Otis", "last_name": "Ratke", "job_title": "Internal Configuration Engineer", "job_type": "Designer", "sex": "female", "ip": "171.94.245.95" }, { "first_name": "Rebecca", "last_name": "Kerluke", "job_title": "Principal Configuration Officer", "job_type": "Officer", "sex": "male", "ip": "219.3.153.26" }, { "first_name": "Gaston", "last_name": "Schmitt", "job_title": "Dynamic Solutions Manager", "job_type": "Consultant", "sex": "male", "ip": "242.26.243.223" }, { "first_name": "Keely", "last_name": "Rippin", "job_title": "Customer Markets Associate", "job_type": "Officer", "sex": "female", "ip": "139.100.249.29" }, { "first_name": "Jabari", "last_name": "Roob", "job_title": "Chief Tactics Liaison", "job_type": "Orchestrator", "sex": "female", "ip": "66.236.109.152" }, { "first_name": "Esta", "last_name": "Krajcik", "job_title": "Regional Implementation Consultant", "job_type": "Designer", "sex": "male", "ip": "149.201.144.125" }, { "first_name": "Kaci", "last_name": "Jenkins", "job_title": "Direct Configuration Engineer", "job_type": "Director", "sex": "female", "ip": "63.4.3.154" }, { "first_name": "Jerrell", "last_name": "Corwin", "job_title": "Direct Program Officer", "job_type": "Producer", "sex": "male", "ip": "227.222.114.140" }, { "first_name": "Carey", "last_name": "Wolf", "job_title": "National Metrics Associate", "job_type": "Manager", "sex": "male", "ip": "64.141.1.229" }, { "first_name": "Emelie", "last_name": "Price", "job_title": "Dynamic Optimization Orchestrator", "job_type": "Supervisor", "sex": "female", "ip": "137.54.159.130" }, { "first_name": "Demetrius", "last_name": "Prohaska", "job_title": "Investor Usability Representative", "job_type": "Manager", "sex": "female", "ip": "185.24.161.238" }, { "first_name": "Henderson", "last_name": "Koch-Paucek", "job_title": "Customer Communications Architect", "job_type": "Technician", "sex": "female", "ip": "246.28.199.148" }, { "first_name": "Aliza", "last_name": "Donnelly", "job_title": "Legacy Group Developer", "job_type": "Facilitator", "sex": "female", "ip": "227.160.180.161" }, { "first_name": "Leo", "last_name": "Considine", "job_title": "Forward Usability Representative", "job_type": "Supervisor", "sex": "male", "ip": "15.63.26.124" }, { "first_name": "Fernando", "last_name": "Shanahan", "job_title": "Global Communications Manager", "job_type": "Associate", "sex": "female", "ip": "14.251.175.43" }, { "first_name": "Hortense", "last_name": "Muller", "job_title": "Customer Communications Strategist", "job_type": "Planner", "sex": "female", "ip": "228.208.250.208" }, { "first_name": "Nicholaus", "last_name": "Little", "job_title": "Chief Division Director", "job_type": "Strategist", "sex": "male", "ip": "27.83.193.114" }, { "first_name": "Litzy", "last_name": "Langosh", "job_title": "Forward Data Planner", "job_type": "Supervisor", "sex": "female", "ip": "194.171.200.146" }, { "first_name": "Buddy", "last_name": "Quitzon", "job_title": "Customer Implementation Developer", "job_type": "Director", "sex": "male", "ip": "163.61.72.73" } ]; const dataArray = dataObj.map(item => Object.values(item));
Tests:
Objects
dataObj.map(obj => objKeysToCamelCase(obj));
Objects with cache
dataObj.map(obj => objKeysToCamelCaseCached(obj));
Arrays
dataArray.map(arr => createObjFromColumns(columns, arr));
Classes from arrays
dataArray.map(arr => createObjFromValues(arr));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Objects
Objects with cache
Arrays
Classes from arrays
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Objects
41514.8 Ops/sec
Objects with cache
88062.6 Ops/sec
Arrays
256022.7 Ops/sec
Classes from arrays
1293780.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark represents a performance comparison of two different approaches for converting data structures with snake_case properties to camelCase properties using JavaScript. The two methods being compared are: 1. **Converting Objects**: - **Method**: This method uses `dataObj`, which is an array of objects. Each object has properties in snake_case (e.g., `first_name`), and the benchmark applies the `objCamelCase` function to convert the keys to camelCase format. - **Implementation**: The function `objCamelCase(obj)` utilizes `Object.entries` to retrieve key-value pairs, and maps over them to apply the `snakeToCamel` function to each key. - **Performance Result**: The execution rate for this method is **19,452.35 executions per second.** - **Pros/Cons**: - **Pros**: Easier to work with when you have complete objects with multiple properties. - **Cons**: Performance drops significantly with larger datasets, likely due to overhead from multiple calls to `Object.entries` and key manipulation. 2. **Converting Arrays**: - **Method**: This method uses `dataArray`, an array of arrays where each nested array corresponds to the values of the properties in a specific order defined by the `columns` array. - **Implementation**: The `objFromColumns(columns, values)` function takes the column definitions and the array values, returning an object with camelCase keys directly. - **Performance Result**: The execution rate for this method is much higher, at **129,400.16 executions per second.** - **Pros/Cons**: - **Pros**: More efficient in terms of performance due to a more straightforward mapping process without needing to manage object key transformation. - **Cons**: Less intuitive when handling data directly as arrays, especially if a dataset has varying lengths or if you need to add or remove fields. ### Other Considerations: - **Ease of Use**: The object method is generally easier to understand and maintain, as data is more naturally represented in object form. - **Scalability**: The array method may scale better for large datasets due to its simpler iteration and absence of additional object manipulation overhead. - **Use of Libraries/Features**: No additional libraries are used in this benchmark, and it primarily relies on JavaScript's built-in array and object methods, such as `Object.entries()` and the spread syntax. ### Alternatives: - **Using a Library**: Libraries like Lodash or Ramda provide utility functions for deep data transformations, including the conversion of keys from snake_case to camelCase. They may handle irregularities and edge cases effectively but can increase bundle size and add complexity. - **Manual Conversion**: Writing a function that manually iterates through keys may provide a tailored solution if specific conditions are met. However, manual methods can become cumbersome and error-prone on larger datasets. - **Using `JSON.stringify` and `JSON.parse`**: This can serve as a simplistic method to transform objects, but would not directly achieve the desired key format change without additional processing. In conclusion, the choice between these two methods largely comes down to the specific use case and performance requirements, as indicated by the significant difference in execution rates. For applications dealing with a high volume of data, the array-based approach is recommended, whereas the object method might suit scenarios with smaller, more manageable datasets.
Related benchmarks:
testuii
Remove First and Last Characters in String
test-string
test-string2
for-in vs object.keys1
Object.keys.length vs for in 2
Object rename: reduce vs fromEntries vs assign & delete
123454
Test 2 function in vanilla js
Comments
Confirm delete:
Do you really want to delete benchmark?