asmcrypto / asmcrypto.js

JavaScript Cryptographic Library with performance in mind.
MIT License
660 stars 182 forks source link

Added support for UTF8 encoding #80

Closed george-ka closed 9 years ago

george-ka commented 9 years ago

Hi Artem, I've created a pull request to add UTF8 support as the most widespread encoding nowadays. Not sure how to add proper tests to the solution, but tested locally. Used "🚀" rocket as an example of 4-byte UTF8 char. Used code samples: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint http://stackoverflow.com/questions/15181809/javascript-charcodeat0-stuck-at-55357 https://gist.github.com/volodymyr-mykhailyk/2923227

Additional info: https://en.wikipedia.org/wiki/UTF-8

vibornoff commented 9 years ago

Hi! Many thanks for your participation.

I've reviewed your pull request and there are some issues.

As jsperf.com has been broken I have to create a small benchmark at jsfiddle.

I slightly modifed your string_to_utf8bytes to achieve at about x4 speedup and its performance becomes competitive w/ original string_to_bytes. So I'm considering to replace original string_to_bytes with UTF-8 capable one.

Unfortunately I'm out of spare time for now. I'll let your pull request in ASAP.

george-ka commented 9 years ago

Wow, it looks like function inlining makes the code much faster, which is obvious, but I didn't take the efficiency into consideration, which is a shame. :) Let me fix it then.

george-ka commented 9 years ago

Following your lead I've got rid of redundant function calls in the second method as well and managed to boost its performance too! Check this out https://jsfiddle.net/kct18qn8/3/ I've updated the pull request with the tweaks.

vibornoff commented 9 years ago

Hmm...

String.fromCharCode.apply( String, strBuffer.subarray(0, j) );

Bad idea.

Guys from Google Closure already shoot their leg once and the solution seems well to me.

george-ka commented 9 years ago

Ok, I'll break it into chunks. I wonder where did they take their magic number (8192 or 8 Kb) from...

george-ka commented 9 years ago

I just couldn't resist the temptation to make batch size a little bigger :)