I just wanted to know if it's faster to use hex color codes or th开发者_运维百科e color names when I want to use it in CSS?
My guess is the codes will be faster since the names are probably assigned to a look-up table and reference hex codes anyway.
That being said, my guess is the speed difference will be completely negligible.
Here's a performance test I just created. They're neck and neck, though hex codes seems to be just barely faster on 4/5 runs so far: http://jsperf.com/css-color-names-vs-hex-codes
I'll go against popular consensus and suggest that it's possible that the color names are faster, if the look-up table returns the color value as an integer. That would mean that when using a named color the browser does not have to manually parse from a string containing hex characters to the corresponding integer, all it needs to do is an O(1) hash look-up. And that may well be slightly faster than parsing from string to integer.
Though as others have rightly noted, in all practical terms the difference between the two will be infinitesimally small. Not something you would ever notice in practice.
Faster in terms of rendering?
In practical terms, there's no possible way it will ever make any difference.
In technical terms, that depends on how the CSS rendering is implemented in each browser. If I had to make a guess, I'd imagine that using hex is about 3 CPU cycles (an exaggeration) faster in most browsers, since the name has to be converted to hex first.
But the time it took you to ask the question represents more time than has been wasted by all browsers in the world doing this conversion since the Internet was invented.
精彩评论