benchmark.js icon indicating copy to clipboard operation
benchmark.js copied to clipboard

Add browserify support.

Open feross opened this issue 9 years ago • 9 comments

The following code fails using the npm package with Browserify:

var benchmark = require('benchmark')
var suite = benchmark.Suite()

With the following error:

Uncaught TypeError: benchmark.Suite is not a constructor

You can quickly see it happening if you use RequireBin and paste the above code.

feross avatar Apr 04 '16 08:04 feross

Hi @feross!

I believe that means lodash isn't being loaded properly.

jdalton avatar Apr 04 '16 08:04 jdalton

@jdalton This was a fresh npm install, and I can confirm that lodash is in the node_modules folder.

feross avatar Apr 04 '16 08:04 feross

Ah ok. Let me know if https://github.com/bestiejs/benchmark.js/commit/7de5705328ec29afa14e8813498e9c6c2a97c0f4 or https://github.com/bestiejs/benchmark.js/commit/c588a6bc349986cfb758486c0b04e8ee256a5ebb helps.

jdalton avatar Apr 04 '16 08:04 jdalton

Yep, works better.

Getting errors on this line

(program):1 Uncaught ReferenceError: Benchmark is not defined

But it doesn't seem to be preventing things from working. It's just spewing in the console at the moment.

feross avatar Apr 04 '16 18:04 feross

I'll keep digging.

jdalton avatar Apr 04 '16 18:04 jdalton

Using Webpack + Babel for my client-side. This thread was helpful and this is what I ended up with that works for me:

// package.json
  "dependencies": {
    "benchmark": "^2.1.3",
    "lodash": "^4.17.4",
  }

// webpack.config.js
  ...
  module: {
    noParse: [
      /benchmark/,
    ]
  },
  ...
import _ from 'lodash';
import process from 'process';

const benchmark = require('benchmark');
const Benchmark = benchmark.runInContext({ _, process });
window.Benchmark = Benchmark;

Seems like the noParse was important.

julianvmodesto avatar Jan 10 '17 16:01 julianvmodesto

Thanks @julianvmodesto !

drewlustro avatar Sep 06 '17 21:09 drewlustro

Result from piping browserify output to chrome headless.

> browserify --bare ./test/perf.js | tape-run

TypeError: Cannot read property 'parentNode' of undefined
    at runScript (http://localhost:52210/bundle.js:902:27)
    at createFunction (http://localhost:52210/bundle.js:755:9)
    at createFunction (http://localhost:52210/bundle.js:762:44)
    at createCompiled (http://localhost:52210/bundle.js:1952:23)
    at clock (http://localhost:52210/bundle.js:1846:58)
    at clock (http://localhost:52210/bundle.js:2056:20)
    at cycle (http://localhost:52210/bundle.js:2245:49)
    at Benchmark.run (http://localhost:52210/bundle.js:2352:13)
    at execute (http://localhost:52210/bundle.js:1098:74)
    at invoke (http://localhost:52210/bundle.js:1208:20)

This issue is caused by the assumption that there will be a script tag in the page (in some cases this is not true)... https://github.com/bestiejs/benchmark.js/blob/c588a6bc349986cfb758486c0b04e8ee256a5ebb/benchmark.js#L663

heapwolf avatar Feb 27 '19 08:02 heapwolf

I am getting the same kind of issue when using Rollup, more info in #244

jonkoops avatar May 20 '21 15:05 jonkoops