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))
}
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))
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Simple Lookup Version | |
With lookup Loop |
Test name | Executions per second |
---|---|
Simple Lookup Version | 3936700.8 Ops/sec |
With lookup Loop | 4869159.0 Ops/sec |
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:
actions
object using the hiByte
value as the key.actions
array and check if the hiByte
value matches.Pros and Cons of Each Approach
actions
object using the hiByte
value.actions
array.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:
Overall, this benchmark appears to be designed to test the performance of different approaches to optimizing a lookup process in a MIDI system.