ArcGIS JavaScript – Selecting All Features from Current Map Extent in ArcGIS JavaScript 3.29

arcgis-javascript-api

I am trying to select all the features which are present in the current extent of map i.e which are visible on screen. I find this link helpful but it iterates through all the graphics and check if that graphic is present in the current extent and adds to result set as shown in code below

dojo.forEach(map.graphics.graphics,function(graphic){
      if (extent.contains(graphic.geometry)) {
        graphic.setSymbol(highlightSymbol);
        results.push(graphic.getContent());
      }

The feature layer of my map consists of thousands of features and I might need selecting very few based on map extent and zoom level. I feel the above code takes lot of time as it needs to iterate through thousands for the few features. I am trying to update the result set whenever the user drags the screen as the map extent change.

Is there any other better way to select features based on the map extent?

Best Answer

In version 4 of a ArcGIS API for JavaScript, you can utilize the FeatureLayerView (sample). I'm not aware of a better way to do what you're wanting to do in the v3 API.