Working with large feature collections

Working with large client-side FeatureLayers, so called feature collections, can be challenging. When naively adding many features at once, your app might be slow or even unresponsive just from the time it takes to generate all the Graphic instances.

This example demonstrates how to add 100,000 features to a feature collection while keeping your app interactive. We achieve this by calling applyEdits with batches of features on an initially empty FeatureLayer. The function uploadFeatures (as shown below) creates and uploads batches of features every 4 milliseconds until there are no more features left to consume.

The function uploadFeatures is in charge of creating and uploading batches of features during a batch time of 4 ms, until there is no more features to consume.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
      /**
       * Function that upload as many features as possible to a
       * client-side FeatureLayer without blocking the UI thread.
       *
       * @param layer - The layer to upload the features to
       * @param iterator - The iterator to consume features
       * @param batchTime - The amount of time during which the iterator can be consumed. By default 4ms
       */
      async function uploadFeatures(layer, iterator, batchTime = 4) {
        const start = performance.now();
        let result = iterator.next();

        while (!result.done) {
          const start = performance.now();
          const features = [];

          // consume for batchTime milliseconds.
          while (performance.now() - start < batchTime && !result.done) {
            features.push(result.value);
            result = iterator.next();
          }

          if (features.length) {
            console.log(`uploading ${features.length} features`);
            await layer.applyEdits({
              addFeatures: features
            });
          }
        }
      }

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.