Domanda

I wonder if there is really nessesary to include the "use strict" when I am done programming and release my JavaScript document to anyone to see. I like to use it because to check that I have coded in a good way.

So, should I include or just remove use "use strict" when I release my JavaScript file for the public?

The reason why I ask is to save space in my JavaScript file.

È stato utile?

Soluzione

I found two opinions about using strict mode in production:

There is no reason to ship “use strict” in your production code. There is no performance gain (verified with V8 team and Brendan a while ago) and I don’t need my users’ VMs doing the extra checks as well. Keep it development only, strip it out during build. This way you also avoid the concatenation issue you reference.

And:

There may not be a performance gain, but there’s also not a performance loss. In production, even more than in development, is where you want to be sure you’re noticing errors. Minimizing the changes between the development and production versions of the code is key to being able to debug issues quickly and efficiently. Yes it helps during development, but there’s no reason to pull it out of production code.

The source is in the comments at the bottom

And of course those 12b weight of "use strict" won't change a thing.

Altri suggerimenti

The line "use strict"; makes up 13 bytes of your file. I'd suggest that this is unlikely to even approach 1% of your file size.

Use one of the many minifiers out there to reduce your file size, along with gzip comression on the server-side, if you're concerned about bandwidth. Manually removing 13 bytes is a false economy.

Exactly which minifier may depend on your code, but here are some suggestions.

Certainly its a micro-optimization, but if you're concatenating (say 25) JS modules together, thats all of the sudden 250bytes.

Deploying to production in a high traffic application, with say 1000 hits per minute, thats 130+ Gb a year of traffic you can prevent if your build removed the 'use strict';s

I'm sure that would save a few bucks on AWS...

I haven't seen a compelling argument to keep it in production other than its not worth the time. It probably isn't, but if you already have a build system, and the know-how to pull this off with minimal effort, why not?

I would currently recommend that you remove the "use strict" in any code that is intended for production (and just use it in debug).

However, I wouldn't remove it just to make the file(s) smaller. The reason I would remove it is because it currently appears to have a negative impact on the actual performance of JavaScript when executed. Hopefully this will change eventually, but for now I would omit it for performance reasons.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top