1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-21 07:38:07 +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:
Tim van der Meij 2017-12-09 17:24:31 +01:00
parent 371ca51402
commit c35bbd11b0
No known key found for this signature in database
GPG key ID: 8C3FD2925A5F2762
3 changed files with 29 additions and 6 deletions

View file

@ -15,7 +15,7 @@
import {
bytesToString, isArrayBuffer, isBool, isEmptyObj, isNum, isSpace, isString,
ReadableStream, removeNullCharacters, stringToBytes, stringToPDFString
log2, ReadableStream, removeNullCharacters, stringToBytes, stringToPDFString
} from '../../src/shared/util';
describe('util', function() {
@ -136,6 +136,20 @@ describe('util', function() {
});
});
describe('log2', function() {
it('handles values smaller than/equal to zero', function() {
expect(log2(0)).toEqual(0);
expect(log2(-1)).toEqual(0);
});
it('handles values larger than zero', function() {
expect(log2(1)).toEqual(0);
expect(log2(2)).toEqual(1);
expect(log2(3)).toEqual(2);
expect(log2(3.14)).toEqual(2);
});
});
describe('stringToBytes', function() {
it('handles non-string arguments', function() {
expect(function() {