jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Use functions + dictionary instead of Switch/Case
// Setup
function onGet(){ /* .. */ }
function onDelete(){ /* .. */ }
function onPost(){ /* .. */ }
var onPut = onPost; // Sharing functionality.
// Extra Setup
var methodHandlers = {
get: onGet,
delete: onDelete,
post: onPost,
put: onPut
};
// Bad
function processRequestWithCase(method){
var requestMethod = method.toLowerCase();
switch(requestMethod){
case "get":
onGet();
break;
case "delete":
onDelete();
break;
// Can be dangerous, also not good for readability
case "post":
case "put":
onPost();
break;
}
}
function processRequestWithDictionary(method){
var requestMethod = method.toLowerCase();
// Get reference to the method so we don't do a lookup twice
var handler = methodHandlers[requestMethod];
// If we have a handle, run it.
if(typeof handler === 'function') handler();
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Using switch/case |
| ready |
Using dictionary/function |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.