简体   繁体   中英

Best practice: multiple CoffeeScript files on a page

I'm developing a page on my rails app that displays analytics data. It has to make several calls to the Flurry Analytics API to retrieve different events. I'm displaying each piece of data in a div with a header and a paragraph (which contains the number retrieved from the analytics and inserted via jQuery).

Is it good practice for me to have a separate CoffeeScript file for each of these divs (8 so far) which calls the API calculates the value and displays it, or is this inefficient?

Additional information: The API I'm calling has a rate limit of one request per second.

Usually it's better to keep your JavaScript code (or CoffeeScript for that matter) in separate files for convenient development, but when deploying on a production server compile them in one file.

A browser will then have to make one request to fetch all your JavaScript instead of making multiple requests (note that modern browsers allow 2-4 concurrent requests per hostname).

This should lower your page loading time.

In performance I can only imagine if there is any difference it's imperceptible, but having a different file for every div sounds like a maintenance nightmare. I'd keep it all in one file simply for ease of editing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM