最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I use curly brackets {} or square brackets [] in this case? - Stack Overflow

programmeradmin7浏览0评论

Currently I have an array using an increasing index:

var idx = 1;
var a = [];
a[idx++] = "apple";
a[idx++] = "orange";
...
console.log(a[2]);

And only accessing it by [], not using array specific functions, like length, indexOf, ...

Apparently following is also working in this case:

var a = {};

So, which one should I prefer in such case? For example any performance difference between them?

Currently I have an array using an increasing index:

var idx = 1;
var a = [];
a[idx++] = "apple";
a[idx++] = "orange";
...
console.log(a[2]);

And only accessing it by [], not using array specific functions, like length, indexOf, ...

Apparently following is also working in this case:

var a = {};

So, which one should I prefer in such case? For example any performance difference between them?

Share Improve this question edited Jul 14, 2014 at 6:18 Deqing asked Jul 10, 2014 at 2:54 DeqingDeqing 14.6k17 gold badges95 silver badges135 bronze badges 5
  • 3 Depends on the data you have and what you want to do with it. If you just have a "list" of things, use an array. Arrays are probably also optimized by the engine. – Felix Kling Commented Jul 10, 2014 at 2:58
  • Thanks @FelixKling, that's what I'd like to know. Don't know why so many downvotes... – Deqing Commented Jul 10, 2014 at 3:04
  • @FelixKling though from memory, most JS implementations also optimise for array-like objects, so they actually have similar performance. Not sure whether this would apply in this instance though, as the first element is 1, not 0. – Qantas 94 Heavy Commented Jul 10, 2014 at 3:08
  • 2 @Deqing: Probably because the question "What does this notation mean?" lacks research effort, you could have found out about object/array literals in every basic tutorial (or by searching StackOverflow). Ask a question "Should I use an array or an object for task XY?" – Bergi Commented Jul 10, 2014 at 3:12
  • @Qantas94Heavy: that could very well be! I ran a couple of tests recently and noticed that there are differences in chrome at least when dealing with large arrays and objects, with and with consecutive keys. But it didn't make a difference in node at all. I just imagined that chrome can't do its hidden class thing with objects like this one. I'm not very familiar with optimizations, so I should probably not even have mentioned it. But it's too late now :) – Felix Kling Commented Jul 10, 2014 at 3:14
Add a comment  | 

2 Answers 2

Reset to default 18

[ ] denotes an array. Arrays only hold values:

var a1 = [1, 2, 3, 4]

As @Qantas pointed out, array can hold more than just values. An array can even contain another array and/or object:

var a2 = [1, 2, ["apple", "orange"], {one: "grape", two: "banana"}];

{ } denotes an object. Objects have key-value pairs like

var a3 = {one: 1, two: 2}

In your case, it's really a matter of how you would like to be able to access the data. If you are only interested in knowing "apple", "pear", etc. Go ahead and use an array. You can access it via it's index

a1[0]; // outputs 1
a1[1]; // outputs 2

or you can iterate over it with a loop. If you use the curly braces, (given the example I gave) you could access it with

a3.one; // outputs 1
a3["two"]; // outputs 2

It's really up to you on how it would best fit your needs in this case. For a more extensive discussion see this article.

The difference is using square brackets will create an Array object while using curly brackets creates a plain object. For example:

a = [];
a[1] = 'a';

b = {};
b[1] = 'b';

a.length; // returns 2
b.length; // is undefined

a.push('z'); // add 'z' to the end of a
b.push('z'); // generates an error - undefined is not a function
             // because plain objects don't have a push method

Read the MDN documentation on Array objects to know more about arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

发布评论

评论列表(0)

  1. 暂无评论