Why require is slow?


I create package atom-ctags, and I found that cost a much load time.

Then I log time cost for load logics, finally found that {BufferedProcess, Point} = require 'atom', Q = require 'q' … require module cost so much time?

I reload or restart atom, require cost the same much time…

So the question is Why require is slow?


This is my understanding of what require does:

  1. Find the file
  2. Load the file
  3. Parse the code
  4. Execute the code

These things take time, especially steps 3 and 4. JavaScript is an interpreted language after all and this incurs a certain overhead cost. To my knowledge, Node doesn’t perform any sort of compiled artifact caching that might speed up the require process when a new session is started.

A number of modules in Atom are designed to not require things until they’re needed. If you look at the Settings View package, you can see an example of this:

Basically, how do I use require?

Thanks for the reply, it seems that javascript can not be compiled into bytecode.
Hope to have a solution for this problem. Now atom obvious start slower than SublimeText.