Loading ...
Sorry, an error occurred while loading the content.

Re: "Use the array literal notation []."

Expand Messages
  • Noah Peters
    ... , ... This is just an example implementation, but is probably representative of how Arrays are implemented in other
    Message 1 of 5 , Jan 30, 2009
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, James Clark <sbj@...> wrote:
      >
      > Douglas Crockford wrote:
      > >
      > >
      > > --- In jslint_com@yahoogroups.com
      <mailto:jslint_com%40yahoogroups.com>,
      > > James Clark <sbj@> wrote:
      > > >
      > > > Part of my project uses a statement like the following:
      > > >
      > > > var b = new Array(a.length);
      > > >
      > > > but jslint complains:
      > > >
      > > > Use the array literal notation [].
      > >
      > > If you can demonstrate a significant performance advantage, then I
      > > will relax the test. Stylistically, I think [] is better, but I will
      > > bow to a compelling performance argument.
      >
      > I did a little experimenting and web-searching. I have found nothing
      > conclusive, but some quickly-thrown-together tests found no significant
      > difference in performance. So for now I will simply initialize the
      > array with [].
      >
      > -jamie
      >

      This is just an example implementation, but is probably representative
      of how Arrays are implemented in other JavaScript libraries.

      http://blogs.msdn.com/jscript/archive/2008/04/08/performance-optimization-of-arrays-part-ii.aspx

      But, basically JavaScript arrays are sparse and not dense arrays,
      therefore the array memory is not pre-allocated.

      -noah
    • Stoyan Stefanov
      One possible pitfall is if by any chance you pass something that is not an integer to the Array constructor new Array(3) is like [undefined, undefined,
      Message 2 of 5 , Mar 6, 2009
      • 0 Attachment
        One possible pitfall is if by any chance you pass something that is not an
        integer to the Array constructor

        new Array(3) is like [undefined, undefined, undefined]
        new Array(Œtest¹) is like [Œtest¹]
        new Array(3.14) is a RangeError

        Best,
        Stoyan

        >> > I'm not exactly sure why using the Array constructor would be bad,
        > except possibly because someone else might have redefined it. So in
        > that case should I be writing this?



        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.