Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Check if Object has Length in JavaScript
(version: 0)
LET THE FUN BEGIN ....
Comparing performance of:
For In vs Object.values
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='resultForIn'></div> <div id='resultValues'></div>
Script Preparation code:
var el1 = document.getElementById('resultForIn'); var el2 = document.getElementById('resultValues');
Tests:
For In
const testObj = { "configGlossary:installationAt": "Philadelphia, PA", "configGlossary:adminEmail": "ksm@pobox.com", "configGlossary:poweredBy": "Cofax", "configGlossary:poweredByIcon": "/images/cofax.gif", "configGlossary:staticPath": "/content/static", "templateProcessorClass": "org.cofax.WysiwygTemplate", "templateLoaderClass": "org.cofax.FilesTemplateLoader", "templatePath": "templates", "templateOverridePath": "", "defaultListTemplate": "listTemplate.htm", "defaultFileTemplate": "articleTemplate.htm", "useJSP": false, "jspListTemplate": "listTemplate.jsp", "jspFileTemplate": "articleTemplate.jsp", "cachePackageTagsTrack": 200, "cachePackageTagsStore": 200, "cachePackageTagsRefresh": 60, "cacheTemplatesTrack": 100, "cacheTemplatesStore": 50, "cacheTemplatesRefresh": 15, "cachePagesTrack": 200, "cachePagesStore": 100, "cachePagesRefresh": 10, "cachePagesDirtyRead": 10, "searchEngineListTemplate": "forSearchEnginesList.htm", "searchEngineFileTemplate": "forSearchEngines.htm", "searchEngineRobotsDb": "WEB-INF/robots.db", "useDataStore": true, "dataStoreClass": "org.cofax.SqlDataStore", "redirectionClass": "org.cofax.SqlRedirection", "dataStoreName": "cofax", "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", "dataStoreUser": "sa", "dataStorePassword": "dataStoreTestQuery", "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", "dataStoreInitConns": 10, "dataStoreMaxConns": 100, "dataStoreConnUsageLimit": 100, "dataStoreLogLevel": "debug", "maxUrlLength": 500 } let temp = 0; for(key in testObj){ temp++; } el1.innerHTML = temp;
Object.values
const testObj = { "configGlossary:installationAt": "Philadelphia, PA", "configGlossary:adminEmail": "ksm@pobox.com", "configGlossary:poweredBy": "Cofax", "configGlossary:poweredByIcon": "/images/cofax.gif", "configGlossary:staticPath": "/content/static", "templateProcessorClass": "org.cofax.WysiwygTemplate", "templateLoaderClass": "org.cofax.FilesTemplateLoader", "templatePath": "templates", "templateOverridePath": "", "defaultListTemplate": "listTemplate.htm", "defaultFileTemplate": "articleTemplate.htm", "useJSP": false, "jspListTemplate": "listTemplate.jsp", "jspFileTemplate": "articleTemplate.jsp", "cachePackageTagsTrack": 200, "cachePackageTagsStore": 200, "cachePackageTagsRefresh": 60, "cacheTemplatesTrack": 100, "cacheTemplatesStore": 50, "cacheTemplatesRefresh": 15, "cachePagesTrack": 200, "cachePagesStore": 100, "cachePagesRefresh": 10, "cachePagesDirtyRead": 10, "searchEngineListTemplate": "forSearchEnginesList.htm", "searchEngineFileTemplate": "forSearchEngines.htm", "searchEngineRobotsDb": "WEB-INF/robots.db", "useDataStore": true, "dataStoreClass": "org.cofax.SqlDataStore", "redirectionClass": "org.cofax.SqlRedirection", "dataStoreName": "cofax", "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", "dataStoreUser": "sa", "dataStorePassword": "dataStoreTestQuery", "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", "dataStoreInitConns": 10, "dataStoreMaxConns": 100, "dataStoreConnUsageLimit": 100, "dataStoreLogLevel": "debug", "maxUrlLength": 500 } el2.innerHTML = Object.values(testObj).length;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For In
Object.values
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):
Let's break down the provided benchmark and explain what's being tested, compared, and analyzed. **Benchmark Overview** The benchmark measures the performance of two approaches: `for...in` loop iteration and `Object.values()` method usage. **Test Case 1: For In Loop Iteration** In this test case, a JavaScript object `testObj` is created with various properties. The script then uses a `for...in` loop to iterate over the object's properties and increments a variable `temp`. Finally, the value of `temp` is assigned to an HTML element using `el1.innerHTML`. **Test Case 2: Object.values() Method Usage** In this test case, the same JavaScript object `testObj` is created as in Test Case 1. The script then uses the `Object.values()` method to get an array of the object's values and assigns its length to an HTML element using `el2.innerHTML`. **Comparison and Analysis** The benchmark compares the performance of these two approaches: 1. **For In Loop Iteration**: This approach iterates over the object's properties, which can be slow due to the potential number of properties. 2. **Object.values() Method Usage**: This approach uses a built-in method to get an array of values, which is generally faster since it's optimized for this specific use case. The benchmark results show that: * Test Case 1 (`For In Loop Iteration`) has an average execution time of around 107,098 executions per second. * Test Case 2 (`Object.values() Method Usage`) has an average execution time of around 233,464 executions per second. **Key Takeaways** * Using `for...in` loop iteration can be slower than using the `Object.values()` method for iterating over object values. * Built-in methods like `Object.values()` are often faster and more efficient than manual loops or other approaches. Overall, this benchmark helps identify performance bottlenecks in JavaScript code and provides guidance on when to use specific approaches to optimize execution time.
Related benchmarks:
test array check
pop vs length-1
check array size with and without neg
check getting length of array
array.length vs array.length > 0 vs array.length !== 0
Comments
Confirm delete:
Do you really want to delete benchmark?