Comment Removal

19 views
Skip to first unread message

Aashish Rangwalla

unread,
Apr 30, 2022, 5:28:47 PM4/30/22
to v8-users
Hi all,
I am considering writing a small piece of code that uses regular expressions to remove all comments and newlines from JS code. This could possibly reduce the memory consumption of V8. Has this already been implemented and where would I put this code?
Thanks,
S. Rangwalla

Jakob Kummerow

unread,
May 1, 2022, 3:44:58 PM5/1/22
to v8-users
I'm not sure why this would reduce memory consumption in V8 -- naturally, V8's parser already disregards comments and whitespace, like any JS engine would.

So I would say the right place for such functionality is a minifier/"uglifier" script that developers can run over their code before shipping it on the web. That way, it also reduces download size. Many such minifiers exist already.

Looking at the details of your plan, two issues come to mind:
(1) Removing newlines from JavaScript is generally not semantics-preserving due to the language's "automatic semicolon insertion" feature.
(2) Regular expressions cannot reliably detect comments in JavaScript, because the language's syntax is more complex than regular expressions can handle. Detecting comments in JS requires a tokenizer that's aware of JS semantics.


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/d5cadd6f-104d-4e8c-a62c-eebbbc837f70n%40googlegroups.com.

Claudia

unread,
May 4, 2022, 10:15:02 AM5/4/22
to v8-users
Would like to mention that there are already tools like https://github.com/mishoo/UglifyJS (ES5-only) and https://github.com/terser/terser (ultimately forked from the former to add support for ES2015+ code) that do more or less what you're proposing here, but go a lot further to minify code by replacing variable names, rearranging code, removing dead code, changing if/else statements into ternaries, and so on. I personally would recommend downloading a proper minifier like one of those and using that instead for the use case you seem to be interested in.
Reply all
Reply to author
Forward
0 new messages