My code works, but it seems to be running exceptionally slow.

I have an array of values to search, and I have a JSON that I'm filtering.

var JSONPull;
var FilterJSON = [];
var TypeIDs = [34,35,36,37,38,39,40,11399,1230,17470,17471,1228,17463,17464,1224,17459,17460,18,17455,17456,1227,17867,17868,20,17452,17453,1226,17448,17449,21,17440,17441,1231,17444,17445,1229,17865,17866,1232,17436,17437,19,17466,17467,1225,17432,17433,1223,17428,17429,22,17425,17426,11396,17869,17870];

fetch('URL API')
    .then(res => res.json())
    .then((out) => {
        JSONPull = out;
        TypeIDs.forEach(function (index){
            FilterJSON = JSONPull.filter((element) => {
                console.log("index: "+index);
                console.log("type_id: "+ element.type_id);
                element.type_id === index;

The console.logs are more just to watch the code while testing, but definitely shouldn't be causing these performance issues.

Thanks in advance for any help/assistance

  • 1
    \$\begingroup\$ logging to console is notoriously slow. If you remove the console.log calls, what's your performance benefit roughly like? \$\endgroup\$
    – Vogel612
    Mar 8, 2020 at 13:01
  • \$\begingroup\$ is type_id is unique? or there are duplicates? if it's unique you can use indexOf / includes to filter. Also after you match an item you can splice the matched typeID out of the array. Because with filter you will walk through entire array even after you have a match. So your complexity is O(n^2) \$\endgroup\$ Mar 9, 2020 at 11:02

1 Answer 1


You are basically making your object filtering into an \$0(m * n)\$ complexity function ( where \$m\$ is number of type id’s) that your are filtering against and \$n\$ is number of objects in the fetched result set. This function should only be \$0(n)\$.

To do this, make type id’s into a Set which allows \$O(1)\$ lookup and then use this Set to filter the fetched objects.

For example:

const typeIds = new Set([ /* ids here */ ]);
const isFilteredType = (element) => typeIds.has(element.type);

// and later when filtering results
.then( (out) => out.filter(isFilteredType) );

It is also customary in javascript to use lowercase first letter for variable names. Your code would look odd to most JS developer for this reason.

There is really not much of a reason to use var in modern JavaScript. You should primarily use const with let being used where the variable needs to be reassignable.

It is not really clear here why JSONpull and FilterJSON are even needed.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.