Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: ES6 class vs function benchmark #11609

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Feb 28, 2017

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

@nodejs-github-bot nodejs-github-bot added the benchmark Issues and PRs related to the benchmark subsystem. label Feb 28, 2017
@mscdex mscdex added the v8 engine Issues and PRs related to the V8 dependency. label Feb 28, 2017
@jasnell
Copy link
Member Author

jasnell commented Feb 28, 2017

@nodejs/v8 @fhinkel ... what's the status on optimizing ES6 class use?

A simple benchmark comparing ES6 classes with idiomatic
ES5 "old style" classes using function and util.inherits
@jasnell
Copy link
Member Author

jasnell commented Feb 28, 2017

What this benchmark does show is that using ES6 Classes is currently significantly faster than the current pattern of using Functions coupled with the instanceof check to catch non-use of new.

millions: [10]
});

class FooBase {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a bit easier to read if these are ES6BaseClass, ES6Subclass, FunctionWithNewCheck...(not necessary to be so verbose, just not Foo Bar)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
benchmark Issues and PRs related to the benchmark subsystem. v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants