Blocking JS from being executed more than once JS lines of code are often executed more than once.
How can this ( annoying ) behaviour be blocked?

    (function ($, Drupal) {
  'use strict';

  Drupal.behaviors.recherche = {
    attach: function (context) {    

      var filters = $('.block-facets-ajax');
      filters.wrapAll('&lt;div class="facets-wrap"&gt;&lt;/div&gt;');
...........


what's expected:

&lt;div class="facets-wrap"&gt;
   &lt;div class="block-facets-ajax&gt;&lt;/div&gt;
   &lt;div class="block-facets-ajax&gt;&lt;/div&gt;      
&lt;/div&gt;


what's happening:

&lt;div class="facets-wrap"&gt;
&lt;div class="facets-wrap"&gt;
&lt;div class="facets-wrap"&gt;
&lt;div class="facets-wrap"&gt;
&lt;div class="facets-wrap"&gt;
&lt;div class="facets-wrap"&gt;
      &lt;div class="facets-wrap"&gt;
           &lt;div class="block-facets-ajax&gt;&lt;/div&gt;
           &lt;div class="block-facets-ajax&gt;&lt;/div&gt;     
        &lt;/div&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


EDIT: trying to apply the doc given in comments, unsuccesfully

Drupal.behaviors.recherche = {
    attach: function (context) {  

      var filters = $('.block-facets-ajax',context);
      filters.once('behave').each(function () {
        $(this).wrapAll('&lt;div class="facets-wrap"&gt;&lt;/div&gt;');
      });


EDIT2 : dependancies added to the library.
        It is not working as it is not entering the .each :

(function ($, Drupal) {
  Drupal.behaviors.myModuleBehavior = {
    attach: function (context, settings) {

      console.log('this is logged');
      $('.block-facets-ajax', context).once('myModuleBehavior').each(function () {
        console.log('this is not logged');
        // Apply the myModuleBehavior effect to the elements only once.
        $(this).wrap('&lt;div class="facets-wrap"&gt;&lt;/div&gt;');
      });

    }
  };
})(jQuery, Drupal);


EDITED 3 :
if i remove context in the selector then .each is executed but instead of wrapping all elt together it 's wrapping each element individually.
This is the case no matter i use wrapAll or wrap method.

(function ($, Drupal) {
  Drupal.behaviors.myModuleBehavior = {
    attach: function (context, settings) {

      $('.block-facets-ajax').once('myModuleBehavior').each(function () {
          $(this).wrap('&lt;div class="facets-wrap"&gt;&lt;/div&gt;');
      });

    }
  };
})(jQuery, Drupal);


what i am trying to achieve :

  &lt;div class="facets-wrap"&gt;
       &lt;div class="block-facets-ajax&gt;&lt;/div&gt;    
       &lt;div class="block-facets-ajax&gt;&lt;/div&gt;     
   &lt;/div&gt;


what's the outcome:

  &lt;div class="facets-wrap"&gt;
       &lt;div class="block-facets-ajax&gt;&lt;/div&gt;
  &lt;/div&gt;
   &lt;div class="facets-wrap"&gt;
       &lt;div class="block-facets-ajax&gt;&lt;/div&gt;     
   &lt;/div&gt;
I adjusted your code to match the one from the docs.

MYMODULE/js/myscripts.js:

(function ($, Drupal) {
  Drupal.behaviors.myModuleBehavior = {
    attach: function (context, settings) {

      $('.block-facets-ajax', context).once('myModuleBehavior').each(function () {
        // Apply the myModuleBehavior effect to the elements only once.
        $(this).wrapAll('&lt;div class="facets-wrap"&gt;&lt;/div&gt;');
      });

    }
  };
})(jQuery, Drupal);




To get that working properly you also need to require core/jquery.once for this script.

MYMODULE.libraries.yml:

myscripts:
  version: VERSION
  js:
    js/myscripts.js: {}
  dependencies:
    - core/jquery
    - core/jquery.once