Skip to content

Commit

Permalink
Tests: Replace fixtures dictionary object with Map [perf]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 16, 2023
1 parent 933fb9e commit 15c2337
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions test/support/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const moduleChildren = module.children,
function createFixtures(files) {
const fixtures = createFixturesFiles(files);
setFixtures(fixtures);
return Object.keys(fixtures);
return [...fixtures.keys()];
}

/**
Expand All @@ -58,9 +58,9 @@ function createFixturesFiles(files) {

// Create object mapping file paths to content
files = conformFiles(files);
const fixtures = Object.create(null);
const fixtures = new Map();
for (const [filename, content] of Object.entries(files)) {
fixtures[pathJoin(fixturePath, filename)] = content;
fixtures.set(pathJoin(fixturePath, filename), content);
}
return fixtures;
}
Expand All @@ -75,7 +75,7 @@ function createFixturesFiles(files) {
* @returns {undefined}
*/
function cleanupFixtures() {
for (const path of Object.keys(getFixtures())) {
for (const path of getFixtures().keys()) {
const module = moduleCache[path];
if (!module) continue;

Expand Down Expand Up @@ -166,7 +166,7 @@ async function serializeInNewProcess(files) {
child.stdout.on('data', (chunk) => { stdout += chunk; });
child.stderr.on('data', (chunk) => { stderr += chunk; });

child.stdin.write(`${JSON.stringify(fixtures)}\n`);
child.stdin.write(`${JSON.stringify([...fixtures])}\n`);

child.on('close', () => {
if (stderr !== '') {
Expand Down
4 changes: 2 additions & 2 deletions test/support/fixturesFs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const {readFileSync, statSync} = fs;
fs.readFileSync = (path, encoding) => {
if (fixtureExists(path)) {
if (encoding !== 'utf8') throw new Error(`Cannot read fixture file with encoding '${encoding}'`);
return fixtures[path];
return fixtures.get(path);
}
return readFileSync(path, encoding);
};
Expand All @@ -66,5 +66,5 @@ fs.statSync = (path, options) => {
};

function fixtureExists(path) {
return !!fixtures && Object.prototype.hasOwnProperty.call(fixtures, path);
return !!fixtures && fixtures.has(path);
}
6 changes: 3 additions & 3 deletions test/support/spawned.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ const {setFixtures} = require('./fixturesFs.js'),
});

// Create virtual fixtures files
const fixtures = JSON.parse(Buffer.concat(buffers));
setFixtures(fixtures);
const fixturesArr = JSON.parse(Buffer.concat(buffers));
setFixtures(new Map(fixturesArr));

// Load first fixture file
// eslint-disable-next-line global-require, import/no-dynamic-require
const input = require(Object.keys(fixtures)[0]);
const input = require(fixturesArr[0][0]);

// Serialize input and output result to stdout
console.log(serialize(input)); // eslint-disable-line no-console
Expand Down

0 comments on commit 15c2337

Please sign in to comment.