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
Find the fastest way of scaling members of a Float32 array. Untyped array just used as comparison point.
The assumption was that assigning something that was stored in a Float32 to a var and only used with other Float32s that the Javascript engine could surmise this and store it as a Float32 rather than a Float64 / double - thus skip any casting between different precisions.
This theory seems to be disproven!
var vector = [1.0, 2.0, 3.0];
var len = 9000;
var arr = new Array(len);
for (var i = 0; i < arr.length; ++i)
arr[i] = vector[i % 3];
var float32array = new Float32Array(arr);
var float64array = new Float64Array(arr);
var float32arrayHalf = new Float32Array([0.5]);
var float32 = float32arrayHalf[0];
function scaleArray(arr) {
for (var i = 0; i < len; i = i + 3) {
arr[i] = arr[i] * 0.5;
arr[1 + i] = arr[1 + i] * 0.5;
arr[2 + i] = arr[2 + i] * 0.5;
}
};
function scaleArrayFloat32(arr) {
for (var i = 0; i < len; i = i + 3) {
arr[i] = arr[i] * 0.5;
arr[1 + i] = arr[1 + i] * 0.5;
arr[2 + i] = arr[2 + i] * 0.5;
}
};
function scaleArrayFloat32Array(arr) {
for (var i = 0; i < len; i = i + 3) {
arr[i] = arr[i] * float32arrayHalf[0];
arr[1 + i] = arr[1 + i] * float32arrayHalf[0];
arr[2 + i] = arr[2 + i] * float32arrayHalf[0];
}
};
function scaleArrayFloat32Inst(arr) {
for (var i = 0; i < len; i = i + 3) {
arr[i] = arr[i] * float32;
arr[1 + i] = arr[1 + i] * float32;
arr[2 + i] = arr[2 + i] * float32;
}
};
Ready to run.
Test | Ops/sec | |
---|---|---|
Untyped |
| ready |
Float32 |
| ready |
Float32Array |
| ready |
Float32Array elem in var |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.