HTML Preparation code:
x
 
1
2
<script src='https://unpkg.com/jquery@3.5.1/dist/jquery.min.js'></script>
3
<script src='https://unpkg.com/jquery-once@2.2.3/jquery.once.min.js'></script>
4
<script>
5
/**
6
* DO NOT EDIT THIS FILE.
7
* See the following change record for more information,
8
* https://www.drupal.org/node/2815083
9
* @preserve
10
**/
11
12
(function () {
13
  var wsRE = /[\11\12\14\15\40]+/;
14
  var attrName = 'data-once';
15
16
  function checkId(id) {
17
    if (typeof id !== 'string') {
18
      throw new TypeError('The once id parameter must be a string');
19
    }
20
21
    if (id === '' || wsRE.test(id)) {
22
      throw new Error('The once id parameter must not be empty or contain spaces');
23
    }
24
25
    return id;
26
  }
27
28
  function checkElement(element) {
29
    if (!(element instanceof Element)) {
30
      throw new TypeError('The element must be an instance of Element');
31
    }
32
33
    return true;
34
  }
35
36
  function filter(elements, selector, apply) {
37
    return Array.prototype.filter.call(elements, function (element) {
38
      var selected = checkElement(element) && element.matches(selector);
39
40
      if (selected && apply) {
41
        apply(element);
42
      }
43
44
      return selected;
45
    });
46
  }
47
48
  function updateAttribute(_ref) {
49
    var value = _ref.value,
50
        add = _ref.add,
51
        remove = _ref.remove;
52
    var result = [];
53
    value.trim().split(wsRE).forEach(function (item) {
54
      if (result.indexOf(item) < 0 && item !== remove) {
55
        result.push(item);
56
      }
57
    });
58
59
    if (add) {
60
      result.push(add);
61
    }
62
63
    return result.join(' ');
64
  }
65
66
  function once(elements, id) {
67
    var dataId = checkId(id);
68
    return filter(elements, ":not([".concat(attrName, "~=\"").concat(dataId, "\"])"), function (element) {
69
      var value = dataId;
70
71
      if (element.hasAttribute(attrName)) {
72
        value = updateAttribute({
73
          value: element.getAttribute(attrName),
74
          add: dataId
75
        });
76
      }
77
78
      element.setAttribute(attrName, value);
79
    });
80
  }
81
82
  once.remove = function (elements, id) {
83
    var dataId = checkId(id);
84
    return filter(elements, "[".concat(attrName, "~=\"").concat(dataId, "\"]"), function (element) {
85
      element.setAttribute(attrName, updateAttribute({
86
        value: element.getAttribute(attrName),
87
        remove: dataId
88
      }));
89
    });
90
  };
91
92
  once.find = function (elements, id) {
93
    var dataId = checkId(id);
94
    return filter(elements, "[".concat(attrName, "~=\"").concat(dataId, "\"]"));
95
  };
96
97
  window.once = once;
98
})();
99
</script>
100
<div id="wrapper"></div>
Script Preparation code:
 
var url = new URL(window.location);
var totalDivs = url.searchParams.get('div') || 10;
var totalOnce = url.searchParams.get('once') || 3;
var wrapper = document.querySelector('#wrapper');
console.log(totalDivs, totalOnce);
wrapper.innerHTML = '<div class="test"></div>'.repeat(totalDivs); 
var items = wrapper.querySelectorAll('div.test');
console.log(items);
Tests:
  • jQuery.once

     
    var $items = jQuery(items);
    for (var i = 0; i < totalOnce; i++) {
      $items.once('test' + i);
    }
  • once

     
    for (var i = 0; i < totalOnce; i++) {
      once(items, 'test' + i);
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    jQuery.once
    once

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 4 years ago)
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0
Firefox 78 on Ubuntu
View result in a separate tab
Test name Executions per second
jQuery.once 36294.7 Ops/sec
once 58390.1 Ops/sec