0

In Node.js and Rust (two languages which I am compiling a custom programming language into), what is the most optimal way to replace a small string in 1000 large files as fast as possible? How should you structure your files to make it optimal? I am specifically thinking about if you change your module name, like in Node.js, and it is imported in 1000 places, how can you most efficiently replace that small string in all 1000 files. It's probably pretty fast to replace 1000 files at 4kb each, but maybe it can be improved.

The string might be inside import { x } from './foo' and you change it to

import { x } from './hello-world'

(the replacement string might be longer or shorter than the original string.

I guess one approach would be to leave some extra space, say 1000 characters for imports, so you can have imports up to 1000 characters long:

import { x } from 'foo'         ...;
import { x } from 'hello-world' ...;

Is that a technique/trick people might use to optimize this? Any other tricks that are standard practice? I looked at Node.js v0.10: Replace certain bytes in file without reading whole file but it appears you can't remove bytes in a file, so it seems you need this sort of extra space.

It gets tricky because you might have other things which you rename too, like import { x } to import { bar }, and everywhere it is used, so maybe what I said isn't a good approach.

Lance
  • 75,200
  • 93
  • 289
  • 503

0 Answers0