Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
two sum
(version: 0)
Comparing performance of:
brute force vs memo vs hash
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
brute force
var twoSum = function(nums, target) { for(let i=0; i<nums.length; i++) { for(let j=i+1; j<nums.length; j++) { if(nums[i]+nums[j]===target) return [i,j] } } return []; }; twoSum([2,7,11,15], 9)
memo
var twoSum = function(nums, target) { let sol = []; for(let i=0; i<nums.length; i++) { if(sol.length) break; for(let j=i+1; j<nums.length; j++) { if(sol.length) break; if(nums[i]+nums[j]===target) sol = [i,j] } } return sol; } twoSum([2,7,11,15], 9)
hash
var twoSumHash = function (nums, target) { let hashTable = [] let pair = [] let i = 0; let foundPair = false while (i < nums.length) { hashTable[nums[i]] = i i += 1 } i = 0 while (i < nums.length && !foundPair) { let compliment = target - nums [i] if (compliment in hashTable) { if (i !== hashTable[compliment]) { pair.push(i) pair.push(hashTable [compliment]) foundPair = true } } i += 1 } return pair } console.log(twoSumHash([2,7,11,15], 9))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
brute force
memo
hash
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
brute force
57758608.0 Ops/sec
memo
49313092.0 Ops/sec
hash
143626.4 Ops/sec
Related benchmarks:
twoSum test
Compare Lodash and JS
builtin plus operator vs. custom sum method
two sum#2
Comments
Confirm delete:
Do you really want to delete benchmark?