/ Javascript

ImmutableJS .toJS() vs .toArray()

General advice is to never convert data from Immutable to native JS types. But if you have to do it use .toArray() rather than .toJS() of course if you work with some array-like data structure.


Immutable is a great library if you need immutability of your code, unfortunately conversion between Immutable and native JS is terribly inefficient process.


Here is my test case created using benchmark library.

const Benchmark = require('benchmark');
var suite = new Benchmark.Suite;

const { List } = require('immutable');
const list = new List([1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]));

// add tests
return suite

.add('toArray()', function() {
  const b = list.toArray();
.add('toJS()', function() {
  const b = list.toJS();

// add listeners
.on('cycle', function(event) {
.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));

You may copy/paste it if you want and run with:

node benchmark.js

Where benchmark.js is a path to the file with mentioned benchmark JS script.

Here are my results:

toArray() x 657,624 ops/sec ±0.99% (89 runs sampled)
toJS() x 189,606 ops/sec ±5.62% (64 runs sampled)
Fastest is toArray()

So try to avoid .toJS() whenever you can.

ImmutableJS .toJS() vs .toArray()
Share this