12

I know there are two ways to convert Array-like objects to Array.

  1. Array.prototype.slice.call(arguments)
  2. Array.from(arguments)

I wonder what's the differences between them , which one should I use to convert Array-like objects.

4
  • 1
    As far as Browser compatibility is concerned, go with the Array.prototype.slice.call(arguments)(Polyfill is available though...) Commented May 3, 2016 at 4:33
  • 2
    Or [...arguments]. Commented May 3, 2016 at 4:40
  • But be careful in old IE 8 and lower. If host objects are passed as this to built–in methods (e.g. passing an HTML collection as this to slice) you will get an error. However, if you have a polyfill for from, that will not be an issue. Commented May 3, 2016 at 4:57
  • Array.prototype.slice.call is very different from Array.from. It's the fastest and most compatible way to create an array from an existing array-like. It works on all browsers including all IE except IE8- when used on host objects such as a NodeList. However ES6's Array.from is much slower because it starts with an array of length 0 which requires log n memory allocations, unlike Array.prototype.slice.call. And it differs because it fills all the gaps with undefined and also creates arrays from iterables. So if you're working with an array-like, use [].slice.call Commented Dec 5, 2023 at 21:45

5 Answers 5

14

Array.prototype.slice.call has been the long-standing mechanism for converting array-like objects to arrays. If you are looking for browser compatibility use this (although it appears that on some older browsers like IE8 and below this will not work at all).

Array.from was introduced ECMA6 in June of 2015. It accomplishes the same thing as the prior mechanism, only in a more fluent and concise manner. In addition Array.from can convert more structures into arrays such as generators.

Learn about array.from

Sign up to request clarification or add additional context in comments.

2 Comments

Actually Array.from accomplishes more, since it can turn generators into arrays etc.
For the record, ECMA-262 is the name of the standard (all versions). ECMAScript is the name of the language. Edition 6 is the current version of ECMA-262, its short name is ECMAScript 2015.
3

Array.prototype.slice.call(arguments) works faster in many browser than Array.from(arguments).

You can look at the results here.

1 Comment

It's remarkable how much faster it is, too. And it's only getting faster in newer version of Chrome.
0

If you want your code to be portable, use the first method. The second method is part of ECMAscript 6 and is therefore not well supported across a range of browsers.

Comments

0

slice works on anything that has a length property, which arguments conveniently does.

Array.from simply convert Array-like or Iterable objects into Array instances.

Unfortunately, Array.from and Rest Parameters are currently only implemented in newer versions of Firefox and Google Chrome

Please refer to link

Comments

0

Array-like objects are objects that have indexed elements. and a length property, similar to arrays, but they may not have all the methods of arrays like push(), pop() & others.

Array destructuring is introduced in ECMAScript (ES6).

 // Example array-like object

const arrayLike = {
  0: 'a',
  1: 'b',
  2: 'c',
  length: 3,
  [Symbol.iterator]: function () {
    let index = 0;
    return {
      next: () => ({
        value: this[index++],
        done: index > this.length
      })
    };
  }
};

// Now it's iterable
const array1 = Array.from(arrayLike);
console.log(array1);  // output: ['a', 'b', 'c']

const array2 = [...arrayLike];
console.log(array2);  // output: ['a', 'b', 'c']

const array3 = Array.prototype.slice.call(arrayLike);
console.log(array3);  // output: ['a', 'b', 'c']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.