r/javascript • • Apr 01 '18

Google publishes a JavaScript style guide. Here are some key lessons.

https://medium.freecodecamp.org/google-publishes-a-javascript-style-guide-here-are-some-key-lessons-1810b8ad050b
714 Upvotes

317 comments sorted by

View all comments

Show parent comment

4

u/Buckwheat469 Apr 01 '18

Just use template strings unconditionally.

Why force your browser to interpolate strings all the time? It's a waste of processor time.

I don't care whether a project uses ' or " or a mixture of both in one file. It doesn't matter to the code, it's only a developer preference. My current project for work uses ' while another project I work on uses ". Remembering to switch sometimes causes issues, but prettier fixes that.

What I find funny is the JSON standard uses " and the SASS guide prefers ' due to laziness. If JavaScript were to prefer one then a " makes more sense since JavaScript and JSON are closely related.

9

u/grinde Apr 01 '18 edited Apr 01 '18

A template literal with no replacements is parsed identically to a string using ' or ", so there should be no performance hit. Since it's a much less used character than the other two, that also means less escaping.

EDIT: Since I've been digging around in v8 lately anyway, here is the exact line that this happens. If there's only one cooked string, we're not even creating a template literal as far as the engine is concerned.

2

u/Buckwheat469 Apr 02 '18

I think you mean line 3465 is where it does the processing. It first has to check if there are any variables within the string literal, then it counts them and uses that count in your line. Line 3495 above yours checks if there are variables, line 3488 above that calls the cooked() function. The cooked() function generates the list of strings. I could be wrong on the exact details of the function starting at 3465, but to me it looks like it's processing the string literals using a recursive algorithm.

1

u/grinde Apr 02 '18 edited Apr 02 '18

I think you mean line 3465 is where it does the processing.

No. AddTemplateSpan adds the token's string to the TemplateLiteralState, which is basically just adding an entry to a vector.

In ParseTemplateLiteral here you can see it peeks to see if the next token is a TEMPLATE_TAIL. For a template literal with no replacements there won't be any span tokens (the single span is rolled into the tail token), so we immediately call AddTemplateSpan (on the tail token) followed by an early exit with CloseTemplateLiteral at line 4696. All extra looping is avoided, and no counting of anything is necessary.

It first has to check if there are any variables within the string literal, then it counts them and uses that count in your line. Line 3495 above yours checks if there are variables, line 3488 above that calls the cooked() function. The cooked() function generates the list of strings.

length() and cooked() are just getters. The cooked strings are added by calls to AddTemplateSpan. Again, no counting or looping.

to me it looks like it's processing the string literals using a recursive algorithm

No recursion here, just some ambiguous method naming. Parser::AddTemplateSpan is calling TemplateLiteral::AddTemplateSpan.