Type hints in V8

25 views
Skip to first unread message

matt0st...@gmail.com

unread,
Jun 2, 2020, 6:43:36 AM6/2/20
to v8-dev
I read an article from 2012 that says V8 tags variables as types internally: https://www.html5rocks.com/en/tutorials/speed/v8/#toc-topic-numbers

Should I add hints for the optimizer to type in the constructor of a class if it's going to be used as either a float, integer, boolean or array respectively later on? By later on I mean used in an initializer function or something.

class Foo {
    constructor() {
        this.x = 0.0; // double
        this.y = 0; // int
        this.z = true; // boolean
        this.w = []; // array
    }
}

Mathias Bynens

unread,
Jun 2, 2020, 6:58:22 AM6/2/20
to v8-...@googlegroups.com
See https://v8.dev/blog/react-cliff:
  • Always initialize your objects in the same way, so that shapes can be effective.
  • Choose sensible initial values for your fields to help JavaScript engines with representation selection.

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/30838539-cae5-41e9-9693-e9f4a472aa6e%40googlegroups.com.

matt0st...@gmail.com

unread,
Jun 2, 2020, 9:37:36 AM6/2/20
to v8-dev
So in the end, it doesn't really matter that much as the React bug mentioned in the article was fixed? And there are no "elegant" ways of defining a double (e.g. let x = 0.0;) unless you use something like 0.000001, Number.MIN_VALUE, -0, or Infinity?


tirsdag 2. juni 2020 12.58.22 UTC+2 skrev Mathias Bynens følgende:
See https://v8.dev/blog/react-cliff:
  • Always initialize your objects in the same way, so that shapes can be effective.
  • Choose sensible initial values for your fields to help JavaScript engines with representation selection.

On Tue, Jun 2, 2020 at 12:43 PM <matt0st...@gmail.com> wrote:
I read an article from 2012 that says V8 tags variables as types internally: https://www.html5rocks.com/en/tutorials/speed/v8/#toc-topic-numbers

Should I add hints for the optimizer to type in the constructor of a class if it's going to be used as either a float, integer, boolean or array respectively later on? By later on I mean used in an initializer function or something.

class Foo {
    constructor() {
        this.x = 0.0; // double
        this.y = 0; // int
        this.z = true; // boolean
        this.w = []; // array
    }
}

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages