Skip to content

Reuse node's implementation of Module._nodeModulePaths#6213

Merged
zcbenz merged 5 commits intomasterfrom
reset-search-paths
Jun 24, 2016
Merged

Reuse node's implementation of Module._nodeModulePaths#6213
zcbenz merged 5 commits intomasterfrom
reset-search-paths

Conversation

@kevinsawicki
Copy link
Copy Markdown
Contributor

@kevinsawicki kevinsawicki commented Jun 23, 2016

Pull request #2976 added support for require not looking outside of the current application when loading modules by patching Module._nodeModulePaths and pulling in some code from node's own module.js implementation.

Node's implementation got some great improvements via nodejs/node#5172 and this pull request seeks to use those improvements via post-processing the results of the default implementation instead of reimplementing the method fully.

  • Add specs for previous behavior
  • Use default Module._nodeModulePaths implementations and filter results in patched version
  • Profile it 🏇

This was profiled by calling Module._nodeModulePaths using 1,000 paths that were all inside the app's resources path so the filtering is happening on every call.

It looks to be a 50-75% speed improvement, results below.

var path = require('path')
var Module = require('module')
var paths = require('./paths')

module.exports = function () {
  var start = Date.now()
  var count = 0
  var total = 0
  for (var i = 0; i < paths.length; i++) {
    count++
    total += Module._nodeModulePaths(paths[i]).length
  }

  var time = Date.now() - start
  console.log(`${paths.length} paths resolved to ${total} search paths in ${time}ms`)

}

Before

screen shot 2016-06-23 at 3 47 37 pm

### After

screen shot 2016-06-23 at 3 48 23 pm

@kevinsawicki kevinsawicki changed the title Reuse node implementation of Module._nodeModulePaths Reuse node's implementation of Module._nodeModulePaths Jun 23, 2016
@zcbenz
Copy link
Copy Markdown
Contributor

zcbenz commented Jun 24, 2016

👍

@zcbenz zcbenz merged commit 552c9b7 into master Jun 24, 2016
@zcbenz zcbenz deleted the reset-search-paths branch June 24, 2016 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants