What can we do if we add a compilation step before the official use of JS Code release.

Source: Internet
Author: User

Compared with compiled languages, JavaScript lacks the compilation process.CodeThe actions for converting executable machine commands are executed by the JS engine in the browser at runtime. But what functions does the modern compiler have in addition to code translation? Can the JS engine cover these tasks at runtime?

The most common problem that cannot be solved during runtime compilation is code compression. Therefore, we have various compressor which makes the Code the smallest when transmitted to the browser.

In this articleArticleGoogle closure complier tells us that we can also unify the OO style during compilation, reduce the object depth to Improve the access speed, use enumeration, constants, and other good features in other languages.

However, after reading the article, I still did not plan to use advanced features such as GCC for inland transportation.

How much can I do during pre-compilation?

I think what makes Google closure complier more meaningful is to tell us that there can be such a pre-compilation step before release, and there can be such a time window. At this time, we can combine our own applications, can we do something based on our own problems.

Scenario 1

The MOD developer develops a function and encapsulates it in mod-.

Then we found that mod-A can be abstracted out of mod-B so that mod-B can be used elsewhere.

So we have mod-B, and mod-A requires mod-B.

The code used by the MOD user is always kslite. Use ('mod-A', function (){});

Since the original mod-A is split, you need to load mod-A and mod-B in sequence.

Because it is in the modular mode of kissylite, it is unclear before mod-A is loaded.

For granular reuse, performance loss is caused-one more request is added and the request is serialized.

Can pre-compilation solve this problem? Yes.

For module users, use the kissylite version of DEBUG (with pre-compilation functions). The console provides two optimization suggestions:

"To allow the sub-module to be loaded in parallel, you should modify the code here to kslite. Use ('mod-a, mod-B ');! "

It can be seen that at least we have solved the problem of parallel loading.

In addition, we can ignore this issue during development. When all functions are ready, we can optimize them. This is actually a function optimized during pre-compilation.

Women's brand ranking
Scenario 2

MoD developers still find that mod-A can be abstracted out of mod-B so that mod-B can be used elsewhere.

This time, he was very clear that the granular processing will introduce more performance problems such as requests and serial loading.

So he is entangled in whether or not to mention mod-B for possible reuse.

Can pre-compilation solve this problem? If the Pre-compilation module can be combined with automated testing and automatic building, you can

First, I want to remove mod-B!

After we run all the important test cases of the current application, we find that all current application scenarios mod-B come in with mod-.

The pre-compilation can provide optimization suggestions.

"To reduce the number of requests, You can append the content of mod-B to the end of mod-".

Because mod-A must be called mod-B, and mod-B is not used independently, it is better to merge.

You can even use the automatic build tool to provide the kissy version kissy4u that is most suitable for the current application.

"To reduce the number of requests, this build has added the mod-B content to the end of mod-:)".

For the baseline kissy version, mod-A and mod-B are independent.

In the kissy4u built for your application, mod-B is in the mod-a file.

You don't have to worry about performance issues introduced by mod-B. Write the most elegant code.

Scenario 3

Or does mod-B need to be output along with mod-.

Now the problem has changed. mod-B may be used independently or called by mod-.

Do you want to merge mod-a mod-B to mod-a.js?

In the end, there are many cases where I first use mod-B and then use mod-? (Obviously, do not merge files)

Or is there a lot of cases of using-A directly? (It's better to merge it)

Can pre-compilation solve this problem? If the Pre-compilation module can combine the statistical feedback data of the actual online usage, you can.

For more information, see manage and optimize Facebook's static webpage resources.

With the feedback of sampling statistics, we can decide whether to merge mod-A and mod-B in the pre-compilation phase based on the feedback.

Scenario 4

Do I need to attach when adding?

An important function of ADD is to register a module. When this module is available without relying on other modules or the content it depends on, it must meet the attach conditions. Do you want to attach?

If attach is enabled, use is faster. If attach is not enabled, page loading is faster.

Q: Do not attach when adding.

By default, code execution consumption introduced by attach is unnecessary. Microsoft also has a dedicated tool, doloto, to split and load the necessary code and unnecessary code.

However, the use process may be slow. In particular, the use may also involve asynchronous module loading...

Following Microsoft's ideas, we can easily find a way To attach modules that may be used in advance when the browser is not busy after domready.

I call this Operation PUSH. What should I push? Are there dozens of functions on the page pushed?

Can pre-compilation solve this problem? Still.

After the page is developed, you can use common functions again. Then the compiler will be able to give a prompt.

"Use ('a, B, c, D') after domready, and attach in advance ".

That is, load the data on demand before domready and pre-push the data after domready.

Pre-compilation opens a window

You can see the specific application for kissylite:

By taking advantage of this link, the module division of module developers does not have to be entangled due to performance issues.

By taking advantage of this step, application developers can configure the best code packaging scheme, as Facebook did.

By taking advantage of this link, the modular architecture does not have to worry about the impact of attach on the page speed when adding, and does not have to attach or drag the slow function.

But it doesn't matter. Maybe no one fits you.

We can see that hedger Wang complained at the beginning of the article, and then solved these problems using gcc.

The scenarios provided here are completely different. Why are they similar.

We also complained about it and, inspired by GCC, solved these problems during the pre-compilation phase it introduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.