Global vs Local

Edit on GitHub

Globally Defining a CustomEvent vs Locally Defining a CustomEvent

Setup

Tests

  • Global

    var element = document.createElement('div');
    var applyCompletion = new CustomEvent('applycompletion', { 'detail': { 'value': null } });
    
    for(var i = 0; i < 100; i++)
    {
        applyCompletion.detail.value = i;
        element.dispatchEvent(applyCompletion);
        applyCompletion.detail.value = null;
    }
  • Local

    var element = document.createElement('div');
    
    function DispatchEvent(i)
    {
        var applyCompletion = new CustomEvent('applycompletion', { 'detail': { 'value': i } });
        element.dispatchEvent(applyCompletion);
    }
    
    for(var i = 0; i < 100; i++)
    {
        DispatchEvent(i);
    }