javascript - What's the advantage of "a,b,c".split(",") over ["a",

admin2025-04-16  2

I've seen this in a couple of places, most notably the plugins.js file of HTML5 Boilerplate, and I'm not sure why.

What's the motivation behind using something like

var d = "header,nav,footer".split(",");

instead of

var d = ["header", "nav", "footer"];

?

I've seen this in a couple of places, most notably the plugins.js file of HTML5 Boilerplate, and I'm not sure why.

What's the motivation behind using something like

var d = "header,nav,footer".split(",");

instead of

var d = ["header", "nav", "footer"];

?

Share Improve this question asked Dec 27, 2011 at 1:36 wlangstrothwlangstroth 1,0706 silver badges12 bronze badges 5
  • 5 It's faster to type, and there's no worrying about mismatched quotes – Ismail Badawi Commented Dec 27, 2011 at 1:38
  • 2 Easier to edit. Is it worth it? Meh. – Dave Newton Commented Dec 27, 2011 at 1:39
  • You say tomato, I say tomato. – Brian Roach Commented Dec 27, 2011 at 1:40
  • 5 @Brian Roach: You say "tomato", I say "t" + "o" + "m" + "a" + "t" + "o". – Asaph Commented Dec 27, 2011 at 1:42
  • 5 @Asaph: You say "tomato", I say [ "t", "o", "m", "a", "t", "o" ].join('') – SLaks Commented Dec 27, 2011 at 1:43
Add a ment  | 

6 Answers 6

Reset to default 12

Under the inexorable pressure of Moore's Law, it's important to find ways for software to consume more CPU cycles to do the same work. Your particular case (using split instead of writing out what you mean in the first place) is an example of "micro-deoptimization".

While there are much more efficient ways to gain inefficiency (code generation templates, preprocessors, and similar tools), it's important for programmers to have a large repertoire of such tricks at hand.

Often people prefer being able to write stuff inside a single string instead of having to write separate strings with quotes etc.

However - when possible - it's usually nicer to do it with space instead of ma since in many languages the split() function will use them as a default delimiter if no arguments are specified.

But all in all - it's just a matter of what the developer prefers to write. It's certainly not faster but the difference does not matter at all (you are not going to call this a billion time anyway, are you?)

It could be the result of an inplete refactoring: Maybe the string originally came from a config file (or there are plans to split it out into a config file), or from user input. In that case the splitting from a string would be natural.

Or it might even to be to match the style from another section which does use this style of input. Maybe like this:

function get_sections()
{
   if has_config()
   {
      return get_config("sections").split(",");
   }
   return "header,nav,footer".split(",");
}

A third option might be to make translation easier.

Not that I'd use it in either of those cases, but it might make sense to some developers.

There is no advantage. In fact, it might be slightly less efficient (I haven't checked) do to the invocation of a method on the string, but probably not by much.

This is just a personal preference. Also, people tend to really get a high using string.split() in any language. It's quite sexy.

Why write x*(1000/4), if you can write x*250? I mean why give the runtime the additional headache of string parsing, splitting (and chance of errors due to missing mas, mas inside the strings etc..) etc? I don't see anything more than some weird coding convention in writing code that way.

Easier to type because you have less quotes. Not suitable for something more plex than a list of words.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744804186a267891.html

最新回复(0)