Comments (2)
Hello, you understant it correctly :), express-minify sets 'a hook' of req.write
so that it could get the entire body before data is sent. It calcs the SHA1 checksum and then try to get it from the cache.
When I first implemented this flow, I had some worries about performance like you :). But later I think I should still implement it like above and here are the reasons:
-
A minifier use
express.static()
is already existed ( express-uglify ). I should not do repeat works. -
Minifying static files only is a little circumscribed. I want to implement a universal, easy-to-use, really simple minification middleware. As you see, user only needs to add a line
app.use(require('express-minify')())
to add minify functions to there apps and remove this line to disable the feature. No need to change any other codes. -
I got this idea from
express.gzip()
. Previously I use gzippo middleware to provide Gzip functions in my app. Gzippo uses an own static file handler. Change code fromexpress.static
togzippo.staticGzip
is simple but I really meet a lot of problems becausegzippo.staticGzip
is NOT as powerful and equivalent asexpress.static
and when I'm using extended functions inexpress.static
, I found it really hard to change these code togzippo.staticGzip
. What's more, ifexpress.static
add something new and the developer ofgzippo
do not implement it, I will unable to use the new functions. The real reason of causing the above problem is,gzippo
did something more then it should did. It should onlygzip
and it shouldn'tserve static
. The function of serving static file should be leaved to other middlewares. Later, I found that expressJs natively provides a gzip middleware:express.gzip()
. It is really simple! Add the line to provide Gzip and remove the line to disable Gzip. This is a real middleware. So I re-implementedexpress-minify
and the flow is similar toexpress.gzip
. -
Why should I consider performance issues? Minifying contents has already became a CPU-cost process. Seting a hook, calcing the checksums, reading from the local FS, these won't cause too much CPU consumption. (May be 'a lot of' memory instead when setting hooks?) If the user uses a memory cache, the only CPU cost is calculating SHA1 and the process is still really fast. If the user uses a FS cache, the bottlenick is I/O and the improvement of I/O performance should be done by the OS. In fact, OS will optimize I/O reading or writing if a block is continually operated (use FS cache).
And, the same as
JSON.stringify
andJSON.parse
in reading cache. These extra conversations of couse will slow down the process. but how much? May be less then 1%. Using 1% of the time to strongly improve reliability, I think this is a good idea. -
What's more, if a user is strongly sensitive to the performance, the best way is to pre-minify contents as I mentioned in README. (Use something like grunt is really a good choice and you can have a try).
express-minify
should not care of this.
from express-minify.
Thanks for your detailed explanation. You are of course correct, my concerns are pretty minor, and my proposed changes would add significant complexity for small gains in efficiency.
I can see the benefit in keeping this project small and simple.If anyone really needs 20,000 requests per second rather than 10,000, then they can make a super-optimized but complex fork of express-minifier, OR as you say not use it at all, in favour of pre-minification!
I was aware of the issues surrounding "replacing" express middleware, but have no experience of trying it. It sounds like it is not so easy to hook into its internals, although I imagine it should be possible to pass a request directly to it when needed.
from express-minify.
Related Issues (20)
- What's the reason why to remove blacklist support? HOT 3
- stylus not required when not loaded yet HOT 2
- NFR: Experimental branch for *UglifyJS2* HOT 10
- NFR: `catch` callbacks, emitters, or equivalent options HOT 9
- Add Brotli support HOT 1
- Middleware not hooked? HOT 1
- Typescript HOT 2
- gzipping should be done after minification HOT 2
- It's not working for me. HOT 41
- js_match.test is not a function HOT 1
- Basic Usage is not working HOT 2
- uglifyJS option and uglifyJS-X compatibility HOT 9
- Crash HOT 2
- CSS are minified, JS not HOT 1
- Question about file cache option HOT 3
- Error while installing on macOS 10.14.1 HOT 2
- How to skip minification for already minified assets? HOT 1
- CSS minifying issue
- Minify and uglify inline script
- `express-minify` +`terser` ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from express-minify.