1 | var bench = require('nanobench');
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 | var through = require('through2');
|
5 |
|
6 | bench('small file × 5', function (b) {
|
7 | warmup(run, function () {
|
8 | b.start();
|
9 |
|
10 | var i = 5;
|
11 | run(function next () {
|
12 | if (--i === 0) b.end();
|
13 | else run(next);
|
14 | });
|
15 | });
|
16 |
|
17 | function run (cb) {
|
18 | smallFile()
|
19 | .pipe(brfs())
|
20 | .on('data', function () {})
|
21 | .on('end', function () {
|
22 | cb();
|
23 | });
|
24 | }
|
25 | });
|
26 |
|
27 | bench('large file without fs', function (b) {
|
28 | warmup(run, function () {
|
29 | b.start();
|
30 |
|
31 | var i = 5;
|
32 | run(function next () {
|
33 | if (--i === 0) b.end();
|
34 | else run(next);
|
35 | });
|
36 | });
|
37 |
|
38 | function run (cb) {
|
39 | largeFile()
|
40 | .pipe(brfs())
|
41 | .on('data', function () {})
|
42 | .on('end', function () {
|
43 | b.end();
|
44 | });
|
45 | }
|
46 | });
|
47 |
|
48 | bench('large file with fs', function (b) {
|
49 | warmup(run, function () {
|
50 | b.start();
|
51 |
|
52 | var i = 5;
|
53 | run(function next () {
|
54 | if (--i === 0) b.end();
|
55 | else run(next);
|
56 | });
|
57 | });
|
58 |
|
59 | function run (cb) {
|
60 | largeFile()
|
61 | .pipe(withFS())
|
62 | .pipe(brfs())
|
63 | .on('data', function () {})
|
64 | .on('end', function () {
|
65 | b.end();
|
66 | });
|
67 | }
|
68 | });
|
69 |
|
70 | function brfs () {
|
71 | return require('../')({
|
72 | fs: {
|
73 | readFileSync: function (path) {
|
74 | return path;
|
75 | }
|
76 | }
|
77 | });
|
78 | }
|
79 |
|
80 | function smallFile () {
|
81 | var r = through();
|
82 | r.end('var a, fs = require("fs"), x; fs.readFileSync("index.js");');
|
83 | return r;
|
84 | }
|
85 | function largeFile () {
|
86 | return fs.createReadStream(path.join(__dirname, './input.js'));
|
87 | }
|
88 | function withFS () {
|
89 | return through(function (chunk, enc, next) {
|
90 | next(null, chunk);
|
91 | }, function (next) {
|
92 | this.push('\nvar a, fs = require("fs"), x; fs.readFileSync("index.js");');
|
93 | next();
|
94 | });
|
95 | }
|
96 |
|
97 | function warmup (run, cb) {
|
98 | var i = 5;
|
99 | run(function next () {
|
100 | if (--i === 0) cb();
|
101 | else run(next);
|
102 | });
|
103 | }
|