Skip to content

Commit

Permalink
perf: Switch to more native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey committed Nov 18, 2024
1 parent 6200229 commit 0db6ab2
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 49 deletions.
4 changes: 2 additions & 2 deletions examples/annotations/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions examples/measure/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/annotationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 || {};
Expand Down
6 changes: 3 additions & 3 deletions src/fetchQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};

/**
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/mapInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/quadFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var $ = require('jquery');
var geo_action = require('./action');
var util = require('./util/common');

Expand Down Expand Up @@ -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);
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
53 changes: 26 additions & 27 deletions tests/cases/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {});
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fetchQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/tileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 0db6ab2

Please sign in to comment.