Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
base64 or json deserialization benchmark
(version: 0)
Comparing performance of:
jsonParse vs danguer vs niklasvh vs typedArrayFrom
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var ab = new ArrayBuffer(50*1024); var uint8 = new Uint8Array(ab); for (var i = 0; i < ab.byteLength; i++) { uint8[i] = i % 256; } var b64 = btoa(uint8) var json = JSON.stringify([...uint8]); function byteArrayToString(bytes) { var CHUNK_SIZE = 8*1024; if (bytes.length <= CHUNK_SIZE) return String.fromCharCode.apply(null, bytes); var str = ''; for (var i = 0; i < bytes.length; i += CHUNK_SIZE) str += String.fromCharCode.apply(null, bytes.slice(i, i+CHUNK_SIZE)); return str; } function validate(res){ if (res !== expected) throw "expected=" + expected + " but got " + res; } function typedArrayFrom(base64_string) { return Uint8Array.from(atob(base64_string), c => c.charCodeAt(0)) } function jsonParse(jsonStr) { return Uint8Array.from(JSON.parse(jsonStr)) } var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // Use a lookup table to find the index. var lookup = new Uint8Array(256); for (var i = 0; i < chars.length; i++) { lookup[chars.charCodeAt(i)] = i; } var niklasvh = function(base64) { var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4; if (base64[base64.length - 1] === "=") { bufferLength--; if (base64[base64.length - 2] === "=") { bufferLength--; } } var arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer); for (i = 0; i < len; i+=4) { encoded1 = lookup[base64.charCodeAt(i)]; encoded2 = lookup[base64.charCodeAt(i+1)]; encoded3 = lookup[base64.charCodeAt(i+2)]; encoded4 = lookup[base64.charCodeAt(i+3)]; bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); } return arraybuffer; }; var danguer = { _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", /* will return a Uint8Array type */ decodeArrayBuffer: function(input) { var bytes = (input.length/4) * 3; var ab = new ArrayBuffer(bytes); this.decode(input, ab); return ab; }, removePaddingChars: function(input){ var lkey = this._keyStr.indexOf(input.charAt(input.length - 1)); if(lkey == 64){ return input.substring(0,input.length - 1); } return input; }, decode: function (input, arrayBuffer) { //get last chars to see if are valid input = this.removePaddingChars(input); input = this.removePaddingChars(input); var bytes = parseInt((input.length / 4) * 3, 10); var uarray; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; var j = 0; if (arrayBuffer) uarray = new Uint8Array(arrayBuffer); else uarray = new Uint8Array(bytes); input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); for (i=0; i<bytes; i+=3) { //get the 3 octects in 4 ascii chars enc1 = this._keyStr.indexOf(input.charAt(j++)); enc2 = this._keyStr.indexOf(input.charAt(j++)); enc3 = this._keyStr.indexOf(input.charAt(j++)); enc4 = this._keyStr.indexOf(input.charAt(j++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; uarray[i] = chr1; if (enc3 != 64) uarray[i+1] = chr2; if (enc4 != 64) uarray[i+2] = chr3; } return uarray; } }
Tests:
jsonParse
jsonParse(json)
danguer
danguer.decodeArrayBuffer(b64)
niklasvh
niklasvh(b64)
typedArrayFrom
typedArrayFrom(b64)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
jsonParse
danguer
niklasvh
typedArrayFrom
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):
I'm ready to help! Please go ahead and ask your question or provide more information about what you'd like me to do with the code and benchmark data you provided. I'll do my best to assist you.
Related benchmarks:
ArrayBuffer to base64 String
String.fromCharCode & btoa vs base64ArrayBuffer function
ArrayBuffer to base64 String vs. superhuman
String.fromCharCode & btoa vs base64ArrayBuffer function FIXED ON LARGE ARRAY
Comments
Confirm delete:
Do you really want to delete benchmark?