aboutsummaryrefslogtreecommitdiff
path: root/files/fr/tools/memory/monster_example
diff options
context:
space:
mode:
authorFlorian Merz <me@fiji-flo.de>2021-02-11 12:36:08 +0100
committerFlorian Merz <me@fiji-flo.de>2021-02-11 12:36:08 +0100
commit39f2114f9797eb51994966c6bb8ff1814c9a4da8 (patch)
tree66dbd9c921f56e440f8816ed29ac23682a1ac4ef /files/fr/tools/memory/monster_example
parent8260a606c143e6b55a467edf017a56bdcd6cba7e (diff)
downloadtranslated-content-39f2114f9797eb51994966c6bb8ff1814c9a4da8.tar.gz
translated-content-39f2114f9797eb51994966c6bb8ff1814c9a4da8.tar.bz2
translated-content-39f2114f9797eb51994966c6bb8ff1814c9a4da8.zip
unslug fr: move
Diffstat (limited to 'files/fr/tools/memory/monster_example')
-rw-r--r--files/fr/tools/memory/monster_example/index.html81
1 files changed, 81 insertions, 0 deletions
diff --git a/files/fr/tools/memory/monster_example/index.html b/files/fr/tools/memory/monster_example/index.html
new file mode 100644
index 0000000000..48357b8d8b
--- /dev/null
+++ b/files/fr/tools/memory/monster_example/index.html
@@ -0,0 +1,81 @@
+---
+title: Exemple d'allocation de monstres
+slug: Outils/Memory/Monster_example
+translation_of: Tools/Memory/Monster_example
+---
+<div>{{ToolsSidebar}}</div><p>Cet article décrit une page web très simple que nous utilisons pour illustrer certaines fonctionnalités de l'outil <a href="/fr/docs/Outils/Memory">Mémoire</a>.</p>
+
+<p>Il est possible de l'essayer sur le site : <a class="external external-icon" href="https://mdn.github.io/performance-scenarios/js-allocs/alloc.html">https://mdn.github.io/performance-scenarios/js-allocs/alloc.html</a>. Voici le code :</p>
+
+<pre class="brush: js">var MONSTER_COUNT = 5000;
+var MIN_NAME_LENGTH = 2;
+var MAX_NAME_LENGTH = 48;
+
+function Monster() {
+
+ function randomInt(min, max) {
+ return Math.floor(Math.random() * (max - min + 1)) + min;
+ }
+
+ function randomName() {
+ var chars = "abcdefghijklmnopqrstuvwxyz";
+ var nameLength = randomInt(MIN_NAME_LENGTH, MAX_NAME_LENGTH);
+ var name = "";
+ for (var j = 0; j &amp;lt; nameLength; j++) {
+ name += chars[randomInt(0, chars.length-1)];
+ }
+ return name;
+ }
+
+ this.name = randomName();
+ this.eyeCount = randomInt(0, 25);
+ this.tentacleCount = randomInt(0, 250);
+}
+
+function makeMonsters() {
+ var monsters = {
+ "friendly": [],
+ "fierce": [],
+ "undecided": []
+ };
+
+ for (var i = 0; i &amp;lt; MONSTER_COUNT; i++) {
+ monsters.friendly.push(new Monster());
+ }
+
+ for (var i = 0; i &amp;lt; MONSTER_COUNT; i++) {
+ monsters.fierce.push(new Monster());
+ }
+
+ for (var i = 0; i &amp;lt; MONSTER_COUNT; i++) {
+ monsters.undecided.push(new Monster());
+ }
+
+ console.log(monsters);
+}
+
+var makeMonstersButton = document.getElementById("make-monsters");
+makeMonstersButton.addEventListener("click", makeMonsters);</pre>
+
+<p>Cette page contient un bouton : Lorsque celui-ci est activé, le code crée des monstres plus précisément :</p>
+
+<ul>
+ <li>Le code crée un objet avec trois propriétés, chacune étant un tableau :
+ <ul>
+ <li>Un pour les monstres "méchants" (fierce).</li>
+ <li>Un pour les monstres "gentils" (friendly).</li>
+ <li>UIn pour les monstres qui n'ont pas encore décidé (undecided).</li>
+ </ul>
+ </li>
+ <li>Pour chaque tableau, le code crée 5000 monstres générés aléatoirement. Chaque monstre a :
+ <ul>
+ <li>Une chaine de caractères, pour le nom du monstre.</li>
+ <li>Un nombre représentant le nombre d'yeux qu'il possède.</li>
+ <li>Un nombre représentant le nombre de tentacules qu'il possède.</li>
+ </ul>
+ </li>
+</ul>
+
+<p>Ainsi, la structure de la mémoire allouée sur la heap JavaScript est un objet contenant trois tableaux contenant chacun 5000 objets (les monstres) chaque objet contient une string et deux int :</p>
+
+<p><a name="allocation-graph"><img alt="" src="https://mdn.mozillademos.org/files/12369/monsters.svg" style="display: block; height: 521px; margin-left: auto; margin-right: auto; width: 500px;"></a></p>