Merge pull request #3013 from gberaudo/performance_vector_source_clear

Improve vector source clear() performance
This commit is contained in:
Éric Lemoine
2014-12-08 14:05:24 +01:00
3 changed files with 57 additions and 10 deletions

View File

@@ -85,11 +85,31 @@ describe('ol.source.Vector', function() {
describe('#clear', function() {
it('removes all features', function() {
it('removes all features using fast path', function() {
var changeSpy = sinon.spy();
goog.events.listen(vectorSource, 'change', changeSpy);
var removeFeatureSpy = sinon.spy();
goog.events.listen(vectorSource, 'removefeature', removeFeatureSpy);
var clearSourceSpy = sinon.spy();
goog.events.listen(vectorSource, 'clear', clearSourceSpy);
vectorSource.clear(true);
expect(vectorSource.getFeatures()).to.eql([]);
expect(vectorSource.isEmpty()).to.be(true);
expect(changeSpy).to.be.called();
expect(changeSpy.callCount).to.be(1);
expect(removeFeatureSpy).not.to.be.called();
expect(removeFeatureSpy.callCount).to.be(0);
expect(clearSourceSpy).to.be.called();
expect(clearSourceSpy.callCount).to.be(1);
});
it('removes all features using slow path', function() {
var changeSpy = sinon.spy();
goog.events.listen(vectorSource, 'change', changeSpy);
var removeFeatureSpy = sinon.spy();
goog.events.listen(vectorSource, 'removefeature', removeFeatureSpy);
var clearSourceSpy = sinon.spy();
goog.events.listen(vectorSource, 'clear', clearSourceSpy);
vectorSource.clear();
expect(vectorSource.getFeatures()).to.eql([]);
expect(vectorSource.isEmpty()).to.be(true);
@@ -97,6 +117,8 @@ describe('ol.source.Vector', function() {
expect(changeSpy.callCount).to.be(1);
expect(removeFeatureSpy).to.be.called();
expect(removeFeatureSpy.callCount).to.be(features.length);
expect(clearSourceSpy).to.be.called();
expect(clearSourceSpy.callCount).to.be(1);
});
});