Skip to content

Conversation

@H4ad
Copy link
Contributor

@H4ad H4ad commented Jan 13, 2024

A weird way to optimize but it works:

Conditions:

  • eol should be \n.
  • range is not supported.
  • keepLines is not supported.

Before:

node test-format-heavy-file.js

End: 2445.59ms
RSS: 2290.83984375MB
Amount of edits: 7876047

node test-format-heavy-file-formatted.js

End: 2560.07ms
RSS: 420.390625MB
Amount of edits: 1

After:

node test-format-heavy-file.js

End: 928.96ms
RSS: 610.75MB
Amount of edits: 1

node test-format-heavy-file-formatted.js

End: 912.79ms
RSS: 890.671875MB
Amount of edits: 1

Benchmarks:

test-format-heavy-file.js
const heavyJson = require('fs').readFileSync('./rrdom-benchmark-1.json', 'utf8');

const lib = require('./lib/umd/main');

const startTime = performance.now();
const edits = lib.format(heavyJson, undefined, {
  tabSize: 2,
  insertFinalNewline: true,
  insertSpaces: true,
});
console.log(`End: ${(performance.now() - startTime).toFixed(2)}ms`);
console.log(`RSS: ${process.memoryUsage.rss() / 1024 / 1024}MB`);
console.log(`Amount of edits: ${edits.length}`);
test-format-heavy-file-formatted.js
const heavyJson = require('fs').readFileSync('./rrdom-benchmark-1-formatted.json', 'utf8');

const lib = require('./lib/umd/main');

const startTime = performance.now();
const edits = lib.format(heavyJson, undefined, {
  tabSize: 2,
  insertFinalNewline: true,
  insertSpaces: true,
});
console.log(`End: ${(performance.now() - startTime).toFixed(2)}ms`);
console.log(`RSS: ${process.memoryUsage.rss() / 1024 / 1024}MB`);
console.log(`Amount of edits: ${edits.length}`);

Assets:

@H4ad
Copy link
Contributor Author

H4ad commented Jan 13, 2024

Before merging, we should also take a look a compare this change with #81 and #82, which also improves the performance but in a more general way than this fast path.

@aeschli
Copy link
Contributor

aeschli commented Jan 22, 2024

The point of the formatter is to provide edits that describe the minimal code changes.
A client can always use JSON.stringify(JSON.parse(..) ) if that is not needed.

@aeschli aeschli closed this Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants