Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
two sum
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
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:
Chrome 132
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
brute force
57758608.0 Ops/sec
memo
49313092.0 Ops/sec
hash
143626.4 Ops/sec
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))