开发者

Which of the following is faster?

开发者 https://www.devze.com 2023-03-27 18:52 出处:网络
Which of the following is faster? And, just by the way, which would you personally prefer? (The position variable stores the CSS position of a DIV element.)

Which of the following is faster? And, just by the way, which would you personally prefer? (The position variable stores the CSS position of a DIV element.)

1)

if (/(relative|absolute|fixed)/).test(position)开发者_运维百科 { ... }

2)

if (!(/^s/).test(position)) { ... }

3)

if (position == 'relative' || position == 'absolute' || position == 'fixed') { ... }

4)

if (position === 'relative' || position === 'absolute' || position === 'fixed') { ... }

5)

if (position != 'static') { ... }

6)

if (position !== 'static') { ... }


Try it out for your self on http://jsperf.com/


number 5 or 6

Regex have a time complexity of O(mn). 3) and 4) require 3 checks each.

Anyway, you should preform each a 1000 time or so a loop and time them, so you have some experimental evidence on your platform.

0

精彩评论

暂无评论...
验证码 换一张
取 消