Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Using Keys vs For Loop v2
(version: 1)
Trying to optimize for speed!
Comparing performance of:
Simple Lookup Version vs With lookup Loop
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
Simple Lookup Version
function getLowBits(byte, numBits){ return ((byte >> numBits << numBits) ^ byte) } let actions = { 0x9: [{id:"note_on", hiByte0: 0x9, loByte0: '', type:"simple", getAction(bytes){return createAction(bytes, this)}}], 0x8: [ {id:"bank_select", hiByte0: 0x9, loByte0: '', secondary:[0x00, 0x20], type:"msblsb", getAction(bytes){return createAction2(bytes, this)}}, {id:"mod_wheel", hiByte0: 0x9, loByte0: '', secondary:[0x01, 0x21], type:"msblsb", getAction(bytes){return createAction2(bytes, this)}} ] } function createAction(bytes, action){ return {id: action.id, channel: getLowBits(bytes[0], 4), type: "", note: bytes[1], volume: bytes[2]} } function createAction2(bytes, action){ if (action.type === "msblsb"){ let i = action.secondary.indexOf(bytes[1]) if (i!==-1){ return {id: action.id, channel: getLowBits(bytes[0], 4), type: i=0?"msb":"lsb", value: bytes[2]} // its a match! dunno if high or low } else { return null // no match } } return null } let midi = [0x85, 0x21, 0x64] //let midi = [0x95, 0x21, 0x64] let hiByte = midi[0]>> 4 let matches = actions[hiByte] let result if (matches.length > 1){ for (let i = 0; i < matches.length; i++) { result = matches[i].getAction(midi); if (result){ //console.log(JSON.stringify(result)) break; } } } else { result = matches[0].getAction(midi) //console.log(JSON.stringify(result)) }
With lookup Loop
function getLowBits(byte, numBits){ return ((byte >> numBits << numBits) ^ byte) } let actions = { 0x9: [{id:"note_on", hiByte0: 0x9, loByte0: '', type:"simple", getAction(bytes){return createAction(bytes, this)}}], 0x8: [ {id:"bank_select", hiByte0: 0x9, loByte0: '', secondary:[0x00, 0x20], type:"msblsb", getAction(bytes){return createAction2(bytes, this)}}, {id:"mod_wheel", hiByte0: 0x9, loByte0: '', secondary:[0x01, 0x21], type:"msblsb", getAction(bytes){return createAction2(bytes, this)}} ] } function createAction(bytes, action){ if (action.type==="simple"){ return {id: action.id, channel: getLowBits(bytes[0], 4), type: "", note: bytes[1], volume: bytes[2]} } } function createAction2(bytes, action){ if (action.type === "msblsb"){ let i = action.secondary.indexOf(bytes[1]) if (i!==-1){ return {id: action.id, channel: getLowBits(bytes[0], 4), type: i=0?"msb":"lsb", value: bytes[2]} // its a match! dunno if high or low } else { return null // no match } } return null } //let midi = [0x85, 0x21, 0x64] let midi = [0x95, 0x21, 0x64] let hiByte = midi[0]>> 4 let matches = actions[hiByte] let result if (matches.length > 1){ for (let i = 0; i < matches.length; i++) { result = matches[i].getAction(midi); if (result){ //console.log(JSON.stringify(result)) break; } } } else { result = matches[0].getAction(midi) //console.log(JSON.stringify(result)) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Simple Lookup Version
With lookup Loop
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare two approaches for optimizing a lookup process in a MIDI (Musical Instrument Digital Interface) system. The goal is to find the fastest way to perform this lookup, which involves searching through an array of actions based on a specific byte value. **Options Being Compared** There are two options being compared: 1. **Simple Lookup Version**: This approach uses a direct lookup in the `actions` object using the `hiByte` value as the key. 2. **With lookup Loop**: This approach uses a loop to iterate through each action in the `actions` array and check if the `hiByte` value matches. **Pros and Cons of Each Approach** 1. **Simple Lookup Version** * Pros: + Fast, as it directly accesses the `actions` object using the `hiByte` value. + Low overhead, as it only requires a single lookup. * Cons: + May not handle cases where there are multiple matches for the same byte value. 2. **With lookup Loop** * Pros: + Can handle cases where there are multiple matches for the same byte value. * Cons: + Slower, as it requires iterating through each action in the `actions` array. + Higher overhead, as it involves more computations. **Library Used** In both test cases, the `getLowBits` function is used to extract the low bits from a byte value. This function is not a standard JavaScript library, but rather a custom implementation specific to this benchmark. **Special JS Feature or Syntax** There doesn't seem to be any special JavaScript features or syntax being used in these benchmarks. The code appears to be standard JavaScript with some custom functions and variables defined specifically for this benchmark. **Other Alternatives** If the goal is to optimize this lookup process, other alternatives could include: * Using a hash table data structure instead of an array. * Caching the results of previous lookups to avoid repeated searches. * Using a more efficient algorithm, such as binary search. Overall, this benchmark appears to be designed to test the performance of different approaches to optimizing a lookup process in a MIDI system.
Related benchmarks:
foreach vs for vs for in
for i < length vs .forEach(t) vs for..of vs for t = keys[i] vs for i =0; i in keys vs for i in object vs .reduce (keys only)
Array.forEach vs Object.keys().forEach
object.keys + lookup + for loop vs. object.entries.forEach
For vs map vs forEach vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?