• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23// We've experienced a regression where the module loader stats a bunch of
24// directories on require() even if it's been called before. The require()
25// should caching the request.
26require('../common');
27const fs = require('fs');
28const assert = require('assert');
29const { fixturesDir } = require('../common/fixtures');
30
31let counter = 0;
32
33// Switch out the two stat implementations so that they increase a counter
34// each time they are called.
35
36const _statSync = fs.statSync;
37const _stat = fs.stat;
38
39fs.statSync = function() {
40  counter++;
41  return _statSync.apply(this, arguments);
42};
43
44fs.stat = function() {
45  counter++;
46  return _stat.apply(this, arguments);
47};
48
49// Load the module 'a' and 'http' once. It should become cached.
50require(`${fixturesDir}/a`);
51require('../fixtures/a.js');
52require('./../fixtures/a.js');
53require('http');
54
55console.log(`counterBefore = ${counter}`);
56const counterBefore = counter;
57
58// Now load the module a bunch of times with equivalent paths.
59// stat should not be called.
60for (let i = 0; i < 100; i++) {
61  require(`${fixturesDir}/a`);
62  require('../fixtures/a.js');
63  require('./../fixtures/a.js');
64}
65
66// Do the same with a built-in module
67for (let i = 0; i < 100; i++) {
68  require('http');
69}
70
71console.log(`counterAfter = ${counter}`);
72const counterAfter = counter;
73
74assert.strictEqual(counterBefore, counterAfter);
75