mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 17:30:09 +02:00
Use native Math
functions in the custom log2
function
It is quite confusing that the custom function is called `log2` while it actually returns the ceiling value and handles zero and negative values differently than the native function. To resolve this, we add a comment that explains these differences and make the function use the native `Math` functions internally instead of using our own custom logic. To verify that the function does what we expect, we add unit tests. All browsers except for IE support `Math.log2` for quite a long time already (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2). For IE, we use the core-js polyfill. According to the microbenchmark at https://jsperf.com/log2-pdfjs/1, using the native functions should also be faster, in my testing almost six times as fast.
This commit is contained in:
parent
371ca51402
commit
c35bbd11b0
3 changed files with 29 additions and 6 deletions
|
@ -600,13 +600,14 @@ function string32(value) {
|
|||
(value >> 8) & 0xff, value & 0xff);
|
||||
}
|
||||
|
||||
// Calculate the base 2 logarithm of the number `x`. This differs from the
|
||||
// native function in the sense that it returns the ceiling value and that it
|
||||
// returns 0 instead of `Infinity`/`NaN` for `x` values smaller than/equal to 0.
|
||||
function log2(x) {
|
||||
var n = 1, i = 0;
|
||||
while (x > n) {
|
||||
n <<= 1;
|
||||
i++;
|
||||
if (x <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return i;
|
||||
return Math.ceil(Math.log2(x));
|
||||
}
|
||||
|
||||
function readInt8(data, start) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue