ArrayBuffer to String Apply Performance
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var bigData = [];
for (var i = 0; i < 512 * 1024; i++) {
bigData.push(i % 256);
}
var bigArrayBuffer = new Uint8Array(bigData).buffer;
var arrayBufferSubarrayToString = function(ab, chunk) {
var len = ab.byteLength;
var u8 = new Uint8Array(ab);
if (len < chunk) {
return String.fromCharCode.apply(null, u8);
} else {
var k = 0,
r = "";
while (k < len) {
// slice is clamped, inclusive of startIndex, exclusive of lastIndex
r += String.fromCharCode.apply(null, u8.subarray(k, k + chunk));
k += chunk;
}
return r;
}
};
var arrayBufferSliceToString = function(ab, chunk) {
var len = ab.byteLength;
if (len < chunk) {
return String.fromCharCode.apply(null, new Uint8Array(ab));
} else {
var k = 0,
r = "";
while (k < len) {
// slice is clamped, inclusive of startIndex, exclusive of lastIndex
r += String.fromCharCode.apply(null, new Uint8Array(ab.slice(k, k + chunk)));
k += chunk;
}
return r;
}
};
var iterateABtoString = function(ab) {
var u8 = new Uint8Array(ab);
var str = "";
for (var i = 0; i < u8.length; i++) {
str += String.fromCharCode(u8[i]);
}
return str;
};
var setAndJoin = function(ab) {
var u8 = new Uint8Array(ab);
var str_array = [];
for (var i = 0; i < u8.length; i++) {
str_array[i] = String.fromCharCode(u8[i]);
}
return str_array.join("");
};
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
32 byte Subarray
|
|
pending… |
8 kB Subarray
|
|
pending… |
32 kB Subarray
|
|
pending… |
64 kB Subarray
|
|
pending… |
Iterate and Append
|
|
pending… |
Set and Join
|
|
pending… |
32 byte Slice
|
|
pending… |
8 kb Slice
|
|
pending… |
32 kB Slice
|
|
pending… |
64 kB Slice
|
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
- Revision 2: published Justin
- Revision 3: published
0 Comments