raphinesse opened a new pull request #242:
URL: https://github.com/apache/cordova-js/pull/242


   ### Platforms affected
   All
   
   
   ### Motivation and Context
   <!-- Why is this change required? What problem does it solve? -->
   <!-- If it fixes an open issue, please link to the issue here. -->
   This improves performance for converting large files to Base64 strings (see 
apache/cordova-plugin-file#364 for more info, especially the extensive 
performance analysis by @LightMind).
   
   Another win here, is that we reduce the code size considerably. A downside 
is that we actually loose a bit of performance for small instances, but I do 
not think that this would be noticeable.
   
   I explored different approaches here, like converting the original algorithm 
to use ArrayBuffers. The problem here is that we still need to convert the end 
result to a string which then becomes a performance bottleneck if you do not 
have support for `TextDecoder` (which we cannot assume with our current ES5 
target).
   
   Fixes #241
   
   
   ### Description
   <!-- Describe your changes in detail -->
   `base64.fromArrayBuffer` now uses `btoa` to convert bytes to a base64 
encoded string. We already use its counterpart `atob` in 
`base64.toArrayBuffer`. Since `btoa` unfortunately operates on binary strings 
instead of buffers, we first need to convert the raw bytes to a [binary 
string](https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary). 
This is the main performance bottleneck here, but applying 
`String.fromCharCode` to large chunks of data works reasonably well.
   
   ### Testing
   <!-- Please describe in detail how you tested your changes. -->
   I added a test that should hopefully preventing people from making stupid 
changes in the future. However, its reliance on an absolute expected runtime 
might lead to problems in the future.
   
   I also did some performance comparisons (ops/s) between the new and old 
version in my local Chrome browser:
   
   |bytes |   old |   new | new/old|
   |------|-------|-------|--------|
   | 1K | 61409 | 33029 |     54%|
   | 32K |  1569 |   913 |     58%|
   | 1M |    11 |    24 |    218%|
   | 32M |    .3 |    .9 |    300%|
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to