Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.1k views
in Technique[技术] by (71.8m points)

JavaScript、TypeScript 中情况判断出现多少次才使用switch语句而不使用if语句 两性能消耗有什么不同

JavaScript、TypeScript 中情况判断出现多少次才使用switch语句而不使用if语句

两者性能消耗有什么不同


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

switch 替代 if 是出于可读性和可扩展性考虑的,性能上的差别可以忽略不计(某些语言中 switch 会被编译器/解释器优化所以性能更高,但 JS 里不是这样)。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...