-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
rusha
with @emotion/hash
and optimize cache key length
`rusha` is 11.5kb & 1500 ops/s `@emotion/hash` is 782b & 60000 ops/s
- Loading branch information
1 parent
10d86ea
commit d376cd2
Showing
5 changed files
with
99 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const emotionHash = require('@emotion/hash') | ||
|
||
let cache = {} | ||
|
||
function hash (string) { | ||
if (!cache[string]) { | ||
cache[string] = emotionHash(string) | ||
} | ||
|
||
return cache[string] | ||
} | ||
|
||
module.exports = hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-env jest */ | ||
const hash = require('../src/hash') | ||
|
||
describe('hash', () => { | ||
it('creates a hash of a string', () => { | ||
expect(hash('something')).toEqual('crsxd7') | ||
expect(hash('something2')).toEqual('nx0tr4') | ||
}) | ||
|
||
it('only calls the underlying hashing function once per value', () => { | ||
const emotionHashMock = jest.fn().mockReturnValue('HASH') | ||
hash.__set__('emotionHash', emotionHashMock) | ||
|
||
expect(hash('something3')).toEqual('HASH') | ||
expect(hash('something3')).toEqual('HASH') | ||
expect(emotionHashMock.mock.calls.length).toEqual(1) | ||
}) | ||
}) |