Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid .join('') followed by .charAt() or .charCodeAt() #97

Closed
JobLeonard opened this issue Jun 26, 2017 · 0 comments
Closed

Avoid .join('') followed by .charAt() or .charCodeAt() #97

JobLeonard opened this issue Jun 26, 2017 · 0 comments

Comments

@JobLeonard
Copy link
Collaborator

JobLeonard commented Jun 26, 2017

So as proposed in this comment, with a fairly trivial refactoring we can change a lot of internal cases where we:

  • construct an array of chars
  • .join('') the array
  • proceed to use .charAt() or .charCodeAt() on the resulting string

.. to leave out the middle step entirely, and replace .charAt() calls with array indexing. For cases where we use .charCodeAt(), what we have to do is replace resultString.charCodeAt(i) with resultArray[i].charCodeAt() - not entirely sure which is faster, but either way it saves the creation of a string. That in itself might save us enough performance to make it worth it, and if we're compressing lots of large data that could matter in memory overhead too.

There might also be a few use-cases in the wild where it's more practical for people to have a returned array of chars instead of a single string, so it could also be a useful (fully backwards compatible) addition to the API.

Like I said, I'm on sick leave at home and currently bored, so I'm going to work on this, both in the lz-string.js and base64-string.js implementations, and push a PR later. Then I leave it up to you to decide if you like the changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant