Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
highestFrequencyString-2
(version: 0)
Write a function named `highestFrequencyString` that takes in an array of strings and returns the string with the highest frequency in the array and how many times it appears.
Comparing performance of:
METHOD MANIA vs FOR LOOP MANIA
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
METHOD MANIA
const highestFrequencyString = (strArray) => { // so janky can we do better? let res = {}; strArray.forEach((str) => (res[str]) ? res[str]++ : res[str]=1); // var reuse because i'm a degenerate res = Object.entries(res).sort((a,b) => b[1]-a[1])[0]; console.log(`${res[0]} appears ${res[1]} times in the array.`); } const fruits = ["apple", "orange", "apple", "peach", "pear", "apple", "peach"]; highestFrequencyString(fruits);
FOR LOOP MANIA
const highestFrequencyString = (strArray) => { strArray.sort((a,b) => a <= b ? -1 : 1); let max = 0; let curr = 0; let res; for (let i = 0; i < strArray.length-1; i++) { if (strArray[i] === strArray[i+1]) { curr++; } else { if (max < ++curr) { max = curr; res = strArray[i]; } curr = 0; } } console.log( `${res} appears ${max} times in the array.`); } const fruits = ["apple", "orange", "apple", "peach", "pear", "apple", "peach"]; highestFrequencyString(fruits);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
METHOD MANIA
FOR LOOP MANIA
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
METHOD MANIA
404238.9 Ops/sec
FOR LOOP MANIA
428746.8 Ops/sec
Related benchmarks:
Object loops: for...in vs Object.keys+forEach (ES5) vs for...of (ES6) vs Object.entries (ES8)
for..of against Object.entries vs for..in
for ++i < length vs .forEach(t) vs for..of vs for t = entries[++i] vs for i = 0; ++i in entries vs for ++i in object vs .reduce (entries)
For vs Foreach copying an array
Counting items in an array ... for vs foreach vs for of
reduce vs for custom
stringCount REDUCE VS FOREACH
Comments
Confirm delete:
Do you really want to delete benchmark?