Skip to content

benchmark: ES6 class vs function benchmark

A simple benchmark comparing ES6 classes with idiomatic ES5 "old style" classes using function and util.inherits

es/class-bench.js millions=10 type="function-new": 51.79236571103209
es/class-bench.js millions=10 type="class": 45.10317389365872
es/class-bench.js millions=10 type="function-new-target": 20.338256568322837
es/class-bench.js millions=10 type="function-new-instanceof": 8.883730047625367
es/class-bench.js millions=10 type="function": 7.686831605297972

In the current codebase, using function() with an instanceof check to catch when new is not used is the most common pattern. As the benchmark shows, the instanceof check is quite expensive. That said, using function without the instanceof check is still faster than using an ES6 class.

Using the --ignition toolchain, the results are quite a bit different:

es/class-bench.js millions=10 type="class": 76.45019775334427
es/class-bench.js millions=10 type="function-new": 45.24199670322474
es/class-bench.js millions=10 type="function-new-target": 27.930478358040915
es/class-bench.js millions=10 type="function-new-instanceof": 8.725080352536871
es/class-bench.js millions=10 type="function": 7.625791694918795

/cc @nodejs/benchmarking @mscdex

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

benchmark

Merge request reports

Loading