Created
September 6, 2012 06:20
-
-
Save srikumarks/3652045 to your computer and use it in GitHub Desktop.
sendMIDIMessage() wrapper speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
var sendMIDIMessage = (function () { | |
var counter = 0; | |
return function (arr) { | |
counter++; | |
if (counter % 1000000 === 0) { | |
console.log(counter + " messages"); | |
} | |
}; | |
}()); | |
var noteOn = (function () { | |
var x = new Uint8Array(3); | |
return function (channel, noteNumber, velocity) { | |
var arr = x; | |
arr[0] = 0x90 + channel; | |
arr[1] = noteNumber; | |
arr[2] = velocity; | |
sendMIDIMessage(arr); | |
}; | |
}()); | |
var noteOn2 = function (channel, noteNumber, velocity) { | |
sendMIDIMessage(0x90 + channel, (noteNumber << 8) | velocity); | |
}; | |
function test(noteOn, N) { | |
for (var i = 0; i < N; ++i) { | |
noteOn(5, 60, 64); | |
} | |
} | |
function timeit(test, fn, N) { | |
var start = Date.now(); | |
test(fn, N); | |
var stop = Date.now(); | |
console.log(stop - start); | |
return stop - start; | |
} | |
document.write(timeit(test, noteOn, 10000000)); | |
document.write("<br/>"); | |
document.write(timeit(test, noteOn2, 10000000)); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment