From 0db6ab2130ebcfb505f078f84061ee03fece209b Mon Sep 17 00:00:00 2001 From: David Manthey Date: Mon, 18 Nov 2024 13:10:53 -0500 Subject: [PATCH] perf: Switch to more native functions --- examples/annotations/main.js | 4 +-- examples/measure/main.js | 4 +-- src/annotationLayer.js | 4 +-- src/fetchQueue.js | 6 ++-- src/mapInteractor.js | 3 +- src/quadFeature.js | 8 +++--- src/registry.js | 3 +- src/util/common.js | 4 +-- tests/cases/annotation.js | 53 ++++++++++++++++++------------------ tests/cases/fetchQueue.js | 2 +- tests/cases/tileLayer.js | 3 +- 11 files changed, 45 insertions(+), 49 deletions(-) diff --git a/examples/annotations/main.js b/examples/annotations/main.js index 4b6098d280..d93275b31f 100644 --- a/examples/annotations/main.js +++ b/examples/annotations/main.js @@ -356,7 +356,7 @@ function handleAnnotationChange(evt) { } var id = entry.attr('annotation-id'); // Remove deleted annotations - if ($.inArray(id, ids) < 0) { + if (!ids.includes(id)) { entry.remove(); return; } @@ -366,7 +366,7 @@ function handleAnnotationChange(evt) { }); // Add if new and fully created $.each(ids, function (idx, id) { - if ($.inArray(id, present) >= 0) { + if (present.includes(id)) { return; } var annotation = layer.annotationById(id); diff --git a/examples/measure/main.js b/examples/measure/main.js index 5406f74e9e..7f470be52b 100644 --- a/examples/measure/main.js +++ b/examples/measure/main.js @@ -541,7 +541,7 @@ function handleAnnotationChange(evt) { } var id = entry.attr('annotation-id'); // Remove deleted annotations - if ($.inArray(id, ids) < 0) { + if (!ids.includes(id)) { entry.remove(); return; } @@ -562,7 +562,7 @@ function handleAnnotationChange(evt) { if (area) { dist = (dist ? dist + ' - ' : '') + area; } - if ($.inArray(id, present) >= 0) { + if (present.includes(id)) { $('#annotationlist .entry[annotation-id="' + id + '"] .entry-dist').text(dist); return; } diff --git a/src/annotationLayer.js b/src/annotationLayer.js index 3ae1606051..1616f89d25 100644 --- a/src/annotationLayer.js +++ b/src/annotationLayer.js @@ -536,7 +536,7 @@ var annotationLayer = function (arg) { */ this.removeAnnotation = function (annotation, update, pos) { if (annotation.id && m_annotationIds[annotation.id()] !== undefined) { - pos = $.inArray(annotation, m_annotations); + pos = m_annotations.indexOf(annotation); if (annotation === m_this.currentAnnotation) { m_this.currentAnnotation = null; } @@ -815,7 +815,7 @@ var annotationLayer = function (arg) { var type = (data.properties || {}).annotationType || feature.featureType, options = Object.assign({}, data.properties || {}), position, datagcs, i, existing; - if ($.inArray(type, annotationList) < 0) { + if (!annotationList.includes(type)) { return; } options.style = options.style || {}; diff --git a/src/fetchQueue.js b/src/fetchQueue.js index df719bb3f3..b34c991f03 100644 --- a/src/fetchQueue.js +++ b/src/fetchQueue.js @@ -144,7 +144,7 @@ var fetchQueue = function (options) { */ this.add = function (defer, callback, atEnd) { if (defer.__fetchQueue) { - var pos = $.inArray(defer, m_this._queue); + var pos = m_this._queue.indexOf(defer); if (pos >= 0) { // m_this._queue.splice(pos, 1); m_this._addToQueue(defer, atEnd, pos); @@ -215,7 +215,7 @@ var fetchQueue = function (options) { * @returns {number} -1 if not in the queue, or the position in the queue. */ this.get = function (defer) { - return $.inArray(defer, m_this._queue); + return m_this._queue.indexOf(defer); }; /** @@ -225,7 +225,7 @@ var fetchQueue = function (options) { * @returns {boolean} `true` if the object was removed. */ this.remove = function (defer) { - var pos = $.inArray(defer, m_this._queue); + var pos = m_this._queue.indexOf(defer); if (pos >= 0) { m_this._queue.splice(pos, 1); return true; diff --git a/src/mapInteractor.js b/src/mapInteractor.js index d9bdde7d30..2a6baa266a 100644 --- a/src/mapInteractor.js +++ b/src/mapInteractor.js @@ -1598,8 +1598,7 @@ var mapInteractor = function (args) { clearState(); // if momentum is enabled, start the action here - if (m_options.momentum.enabled && - $.inArray(oldAction, m_options.momentum.actions) >= 0) { + if (m_options.momentum.enabled && m_options.momentum.actions.includes(oldAction)) { var t = (new Date()).valueOf(); var dt = t - m_mouse.time + m_mouse.deltaTime; if (t - m_mouse.time < m_options.momentum.stopTime) { diff --git a/src/quadFeature.js b/src/quadFeature.js index f6c776f114..1989a21f94 100644 --- a/src/quadFeature.js +++ b/src/quadFeature.js @@ -466,8 +466,8 @@ var quadFeature = function (arg) { prev_onload = image.onload; image.onload = function () { if (previewColor !== undefined) { - if ($.inArray(quad, clrQuads) >= 0) { - clrQuads.splice($.inArray(quad, clrQuads), 1); + if (clrQuads.includes(quad)) { + clrQuads.splice(clrQuads.indexOf(quad), 1); } delete quadinfo.clrquad; } @@ -544,8 +544,8 @@ var quadFeature = function (arg) { prev_onload = video.onloadeddata; video.onloadeddata = function () { if (previewColor !== undefined) { - if ($.inArray(quad, clrQuads) >= 0) { - clrQuads.splice($.inArray(quad, clrQuads), 1); + if (clrQuads.includes(quad)) { + clrQuads.splice(clrQuads.indexOf(quad), 1); } delete quadinfo.clrquad; } diff --git a/src/registry.js b/src/registry.js index 0e14a2ed63..c1acb14dee 100644 --- a/src/registry.js +++ b/src/registry.js @@ -1,4 +1,3 @@ -var $ = require('jquery'); var geo_action = require('./action'); var util = require('./util/common'); @@ -460,7 +459,7 @@ registry.featuresForAnnotations = function (annotationList) { features.push(feature); } else { annotationList[ann].forEach(function (state) { - if ($.inArray(state, annotations[ann].features[feature]) >= 0) { + if (annotations[ann].features[feature].includes(state)) { features.push(feature); } }); diff --git a/src/util/common.js b/src/util/common.js index a21a576f5c..065980688d 100644 --- a/src/util/common.js +++ b/src/util/common.js @@ -464,7 +464,7 @@ var util = { do { found = util.hasAction(actions, action, name, owner); if (found) { - actions.splice($.inArray(found, actions), 1); + actions.splice(actions.indexOf(found), 1); removed += 1; } } while (found); @@ -1010,7 +1010,7 @@ var util = { continue; } const copy = source[key]; - if (copy && typeof copy === 'object' && copy && (copy.constructor === undefined || copy.constructor === Object || Array.isArray(copy))) { + if (copy && typeof copy === 'object' && (copy.constructor === Object || Array.isArray(copy))) { let src = target[key]; if (!Array.isArray(copy)) { if (typeof src !== 'object' || Array.isArray(src)) { diff --git a/tests/cases/annotation.js b/tests/cases/annotation.js index f01beab6ed..6dae308e4d 100644 --- a/tests/cases/annotation.js +++ b/tests/cases/annotation.js @@ -3,7 +3,6 @@ describe('geo.annotation', function () { 'use strict'; - var $ = require('jquery'); var geo = require('../test-utils').geo; var createMap = require('../test-utils').createMap; var destroyMap = require('../test-utils').destroyMap; @@ -1716,22 +1715,22 @@ describe('geo.annotation', function () { var newshapeCount = 0; it('listAnnotations', function () { var list = geo.listAnnotations(); - expect($.inArray('rectangle', list) >= 0).toBe(true); - expect($.inArray('polygon', list) >= 0).toBe(true); - expect($.inArray('point', list) >= 0).toBe(true); - expect($.inArray('line', list) >= 0).toBe(true); - expect($.inArray('unknown', list) >= 0).toBe(false); + expect(list.includes('rectangle')).toBe(true); + expect(list.includes('polygon')).toBe(true); + expect(list.includes('point')).toBe(true); + expect(list.includes('line')).toBe(true); + expect(list.includes('unknown')).toBe(false); }); it('registerAnnotation', function () { var func = function () { newshapeCount += 1; return 'newshape return'; }; sinon.stub(console, 'warn').callsFake(function () {}); - expect($.inArray('newshape', geo.listAnnotations()) >= 0).toBe(false); + expect(geo.listAnnotations().includes('newshape')).toBe(false); expect(geo.registerAnnotation('newshape', func)).toBe(undefined); - expect($.inArray('newshape', geo.listAnnotations()) >= 0).toBe(true); + expect(geo.listAnnotations().includes('newshape')).toBe(true); expect(console.warn.calledOnce).toBe(false); expect(geo.registerAnnotation('newshape', func).func).toBe(func); expect(console.warn.calledOnce).toBe(true); - expect($.inArray('newshape', geo.listAnnotations()) >= 0).toBe(true); + expect(geo.listAnnotations().includes('newshape')).toBe(true); console.warn.restore(); }); it('createAnnotation', function () { @@ -1745,29 +1744,29 @@ describe('geo.annotation', function () { }); it('featuresForAnnotations', function () { var features = geo.featuresForAnnotations(['polygon']); - expect($.inArray('polygon', features) >= 0).toBe(true); - expect($.inArray('line.basic', features) >= 0).toBe(true); - expect($.inArray('point', features) >= 0).toBe(false); + expect(features.includes('polygon')).toBe(true); + expect(features.includes('line.basic')).toBe(true); + expect(features.includes('point')).toBe(false); features = geo.featuresForAnnotations({polygon: true}); - expect($.inArray('polygon', features) >= 0).toBe(true); - expect($.inArray('line.basic', features) >= 0).toBe(true); - expect($.inArray('point', features) >= 0).toBe(false); + expect(features.includes('polygon')).toBe(true); + expect(features.includes('line.basic')).toBe(true); + expect(features.includes('point')).toBe(false); features = geo.featuresForAnnotations({polygon: [geo.annotation.state.done]}); - expect($.inArray('polygon', features) >= 0).toBe(true); - expect($.inArray('line.basic', features) >= 0).toBe(false); - expect($.inArray('point', features) >= 0).toBe(false); + expect(features.includes('polygon')).toBe(true); + expect(features.includes('line.basic')).toBe(false); + expect(features.includes('point')).toBe(false); features = geo.featuresForAnnotations({polygon: [geo.annotation.state.done, geo.annotation.state.create]}); - expect($.inArray('polygon', features) >= 0).toBe(true); - expect($.inArray('line.basic', features) >= 0).toBe(true); - expect($.inArray('point', features) >= 0).toBe(false); + expect(features.includes('polygon')).toBe(true); + expect(features.includes('line.basic')).toBe(true); + expect(features.includes('point')).toBe(false); features = geo.featuresForAnnotations(['polygon', 'point']); - expect($.inArray('polygon', features) >= 0).toBe(true); - expect($.inArray('line.basic', features) >= 0).toBe(true); - expect($.inArray('point', features) >= 0).toBe(true); + expect(features.includes('polygon')).toBe(true); + expect(features.includes('line.basic')).toBe(true); + expect(features.includes('point')).toBe(true); features = geo.featuresForAnnotations(['polygon', 'unknown']); - expect($.inArray('polygon', features) >= 0).toBe(true); - expect($.inArray('line.basic', features) >= 0).toBe(true); - expect($.inArray('point', features) >= 0).toBe(false); + expect(features.includes('polygon')).toBe(true); + expect(features.includes('line.basic')).toBe(true); + expect(features.includes('point')).toBe(false); }); it('rendererForAnnotations', function () { sinon.stub(console, 'warn').callsFake(function () {}); diff --git a/tests/cases/fetchQueue.js b/tests/cases/fetchQueue.js index 5e3f80b615..44c67aa2b5 100644 --- a/tests/cases/fetchQueue.js +++ b/tests/cases/fetchQueue.js @@ -204,7 +204,7 @@ describe('geo.core.fetchQueue', function () { expect(q.get(dlist[qrefs[i][j] - 1])).toBe(j); } for (j = 0; j < dlist.length; j += 1) { - expect(q.get(dlist[j])).toBe($.inArray(dlist[j].ref, qrefs[i])); + expect(q.get(dlist[j])).toBe(qrefs[i].indexOf(dlist[j].ref)); } } diff --git a/tests/cases/tileLayer.js b/tests/cases/tileLayer.js index 7a80c09fea..d3373262ef 100644 --- a/tests/cases/tileLayer.js +++ b/tests/cases/tileLayer.js @@ -1430,8 +1430,7 @@ describe('geo.tileLayer', function () { tiles = l._getTiles(1, {left: 50, right: 500, bottom: 500, top: 50}); expect(tiles.length).toBe(5); tiles.forEach(function (tile) { - expect($.inArray(tile._url.split('?s=')[1].split('&')[0], - subdomains)).toBeGreaterThan(-1); + expect(subdomains.includes(tile._url.split('?s=')[1].split('&')[0])).toBe(true); expect(tile._url.split('&x')[1]).toBe('=' + tile.index.x + '&y=' + tile.index.y + '&z=' + tile.index.level); });