From e7d37afaab7f02344ae9c2c518d124d68ab0af46 Mon Sep 17 00:00:00 2001 From: Alejandro Such Date: Thu, 1 Jun 2017 17:31:33 +0200 Subject: [PATCH] Add passive event listener if available, to make the page more responsive. --- src/jqLite.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/jqLite.js b/src/jqLite.js index ad59fbd1af2b..e1343cabbe66 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -851,6 +851,21 @@ function specialMouseHandlerWrapper(target, event, handler) { // These functions chain results into a single // selector. ////////////////////////////////////////// +function passiveOptionSupported() { + var supportsPassiveOption = false; + try { + var opts = Object.defineProperty({}, 'passive', { + get: function() { + supportsPassiveOption = true; + } + }); + window.addEventListener('test', null, opts); + // eslint-disable-next-line no-empty + } catch (e) { + } + return supportsPassiveOption; +} + forEach({ removeData: jqLiteRemoveData, @@ -881,7 +896,7 @@ forEach({ eventFns = events[type] = []; eventFns.specialHandlerWrapper = specialHandlerWrapper; if (type !== '$destroy' && !noEventListener) { - element.addEventListener(type, handle); + element.addEventListener(type, handle, passiveOptionSupported() ? { passive: true } : false); } }