Use global theme javascript only in a few occasions. You should make sure javascript is only used on the right places. This snippet shows you how to load javascript when a specific view is loaded only.
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_pre_render().
*/
function mymodule_views_pre_render(ViewExecutable $view) {
if (isset($view) && ($view->storage->id() == 'view_id')) {
$view->element['#attached']['library'][] = 'mymodule/MYLIBRARY';
}
}