Last active
May 16, 2021 22:32
-
-
Save cpojer/56ab736128a1af55b8075e98164926d8 to your computer and use it in GitHub Desktop.
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
// 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'); |
const {createRequire} = require('module');
const envRequire = createRequire(require.resolve('jest-environment-jsdom'));
const stringHelpers = envRequire('jsdom/lib/jsdom/living/helpers/strings.js')`;
is probably safer so you won't have to rely on on FS layout of node_modules
https://nodejs.org/api/module.html#module_module_createrequire_filename
Ah, good point – I'm operating in a place where there are multiple jsdoms.
There's an extra backtick on line 10
Thanks, copy paste mistake, fixed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context and explanation: https://twitter.com/cpojer/status/1393518022296539136