8000 Slow ES6 iterator on Set · Issue #1249 · immutable-js/immutable-js · GitHub
[go: up one dir, main page]

Skip to content
Slow ES6 iterator on Set #1249
Closed
Closed
@applitopia

Description

@applitopia

What happened

Set has an extra map((_, k) => k) transformation in implementation of __iterator(). It causes reify() to recreate the whole collection from scratch.

  __iterator(type, reverse) {
    return this._map.map((_, k) => k).__iterator(type, reverse);
  }

It is noticeable on sets with higher number of items.
In our fork https://github.com/applitopia/immutable-sorted we fixed it by changing the add() function as follows:

   add(value) {
-    return updateSet(this, this._map.set(value, true));
+    return updateSet(this, this._map.set(value, value));
   }

How to reproduce

const { Set, Map, Range } = require('immutable');

const time = function(s, f) {
	const ts1 = Date.now();

	f();

	const	ts2 = Date.now();
	const tm = Math.floor(ts2-ts1)/1000;
	console.log("%s: %ds", s, tm);
};

time("Set", ()=>Range(0,1000000).toSet());
time("Set+__iterator", ()=>Range(0,1000000).toSet().__iterator());
time("Map", ()=>Range(0,1000000).toKeyedSeq().toMap());
time("Map+__iterator", ()=>Range(0,1000000).toKeyedSeq().toMap().__iterator());

Output:

Set: 0.777s
Set+__iterator: 1.343s
Map: 0.611s
Map+__iterator: 0.55s

The time needed for Set+__iterator has doubled.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0