Skip to content

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

@bjohansebas bjohansebas changed the base branch from master to v2 April 27, 2025 03:36
Copy link
Member

@bjohansebas bjohansebas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, We don't do microbenchmarks to measure performance, but I think it's a good change.

@bjohansebas bjohansebas merged commit 47692c6 into expressjs:v2 May 16, 2025
10 checks passed
@UlisesGascon
Copy link
Member

I was trying some benchmark using an online tool (https://jsbm.dev/Gj7TmID5PaN75) and the results seems possitive 🥳

expressjssession1034

Not sure why the CI was not triggered 🤔

@cesco69
Copy link
Author

cesco69 commented May 16, 2025

I was trying some benchmark using an online tool (https://jsbm.dev/Gj7TmID5PaN75) and the results seems possitive 🥳

Wow!

When a new release?

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.

3 participants