Several months ago, my boss asked me to create a 5000 rows report. After I
solved all problems related to business and server side performance, I’m still
not satisfied with the HTML size. My report has to download about 20Mb HTML.
Thank god, I can use gzip to make it smaller than 1MB. However I still think I
can make it smaller by removing all unneeded whitespaces, similar as the
minified javascript.
I found a solution in
Arran Maclean’s blog.
He used Action Filter to remove all unneeded whitespaces after ASP.MVC 4 render
the html files.
His Action Filter renders the perfect minified html file, all unneeded whitespaces
are removed. However, I think it consumes a little memory and CPU resource to
remove all whitespaces of a 20MB file.
Why we don’t remove the whitespace of razor view? I have researched several
hours in Google but nobody
thought like me. So I decide to write small winform
application to remove all whitespace of my razor view before I deploy my
project.
The original Razor view:
The Razor view hash been removed whitespaces:
Here are my test results.
The “No trimming”
section
indicating that the server returns a 11.1MB file in
about 1.85-3.45 seconds. The “Runtime trimming”
section
indicating that the
server removes all whitespaces by Action Filter and returns a 6.4MB file in
about 3.36-4.07 seconds. The last
section
indicating that the server renders HTML
from a minified razor view and returns a 6.5MB file in 1.35-2.71 seconds.
My application doesn’t have the best algorithm to minify the razor view so I
can’t afford the best result. However my solution reduces a little consumed
resource of server
You can get my source code at
here:
https://github.com/haison8x/razor-white-space-trimming. You are welcome for improving my algorithm and quoting my blog.