Skip to content

perf: 2x faster hash stringify #1034

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

cesco69
Copy link

@cesco69 cesco69 commented Apr 14, 2025

With huge session object the hash method become slower because iterate on each key/value of the object, just for remove the cookie property on the first level!

This PR make hash method 2/3x faster!

Online benchmark

This PR use the spread operator available on Node.js >= 8.3

if we need to support old NodeJS, I can introduce a new options for cusom hash function, eg:

app.use(session({
  hash: function(sess) {
    // serialize
    const { cookie, ...sessWithoutCookie } = sess;
    const str = JSON.stringify(sessWithoutCookie);

    // hash
    return crypto
      .createHash('sha1')
      .update(str, 'utf8')
      .digest('hex')
  },
  secret: 'keyboard cat'
}))

In this case, close this PR and merge this one #1035

Side note: this new options, also can close #990

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

Successfully merging this pull request may close these issues.

1 participant