Last active
May 16, 2021 22:32
This file contains hidden or 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
| // Add memoization to two string helpers in jsdom that are called | |
| // tens of thousands of times on the same strings and slow tests | |
| // down by up to 3x. | |
| // | |
| // See https://github.com/jsdom/jsdom/commit/63d24a06d04a60279599782dc97899cde59d901d#diff-a29b6d0a417180220d299dde53ac3e1f820c3ade3daec396118f27c4270e1457R137 | |
| const {createRequire} = require('module'); | |
| const envRequire = createRequire(require.resolve('jest-environment-jsdom')); | |
| const stringHelpers = envRequire('jsdom/lib/jsdom/living/helpers/strings.js'); | |
| const memoize = (fn) => { | |
| const map = new Map(); | |
| return (string) => { | |
| if (map.has(string)) { | |
| return map.get(string); | |
| } | |
| const value = fn(string); | |
| map.set(string, value); | |
| return value; | |
| }; | |
| }; | |
| stringHelpers.asciiUppercase = memoize(stringHelpers.asciiUppercase); | |
| stringHelpers.asciiLowercase = memoize(stringHelpers.asciiLowercase); | |
| module.exports = require('jest-environment-jsdom'); |
Author
Ah, good point – I'm operating in a place where there are multiple jsdoms.
There's an extra backtick on line 10
Author
Thanks, copy paste mistake, fixed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is probably safer so you won't have to rely on on FS layout of
node_moduleshttps://nodejs.org/api/module.html#module_module_createrequire_filename