Same comment about forEach() here, but in this case it is much worse because tags has so many keys.

This is really inefficient. If you don't change to use only the numeric form of each tag, you need a data structure to do the reverse lookup quickly instead of having to iterate for each one.  So you'll need to create an object that maps from tag name to tag number.