You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are2old contexts now and 1 currently running for`pow(2, 1)`.
217
+
Acum există2contexte vechi și 1 în curs de desfășurare pentru`pow(2, 1)`.
218
218
219
-
### The exit
219
+
### Ieșirea
220
220
221
-
During the execution of`pow(2, 1)`, unlike before, the condition `n == 1`is truthy, so the first branch of`if`works:
221
+
În timpul execuției lui`pow(2, 1)`, spre deosebire de înainte, condiția `n == 1`este truthy, astfel încât prima ramură din`if`funcționează:
222
222
223
223
```js
224
224
function pow(x, n) {
@@ -232,42 +232,42 @@ function pow(x, n) {
232
232
}
233
233
```
234
234
235
-
There are no more nested calls, so the function finishes, returning `2`.
235
+
Nu mai există alte apeluri nested, așa că funcția se termină, returnând`2`.
236
236
237
-
As the function finishes, its execution context is not needed anymore, so it's removed from the memory. The previous one is restored off the top of the stack:
237
+
Deoarece funcția se termină, contextul său de execuție nu mai este necesar, așa că este șters din memorie. Cel anterior este restaurat din vârful stack-ului:
238
238
239
239
240
240
<ul class="function-execution-context-list">
241
241
<li>
242
-
<span class="function-execution-context">Context: { x: 2, n: 2, at line 5 }</span>
242
+
<span class="function-execution-context">Context: { x:2, n:2, la linia5 }</span>
The execution of`pow(2, 2)` is resumed. It has the result of the subcall `pow(2, 1)`, so it also can finish the evaluation of`x * pow(x, n - 1)`, returning`4`.
251
+
Se reia execuția lui `pow(2, 2)`. Ea are rezultatul subapelării `pow(2, 1)`, astfel încât poate termina evaluarea lui `x * pow(x, n - 1)`, returnând`4`.
252
252
253
-
Then the previous context is restored:
253
+
Apoi se restabilește contextul anterior:
254
254
255
255
<ul class="function-execution-context-list">
256
256
<li>
257
-
<span class="function-execution-context">Context: { x:2, n:3, at line5 }</span>
257
+
<span class="function-execution-context">Context: { x:2, n:3, la linia5 }</span>
When it finishes, we have a result of`pow(2, 3) = 8`.
262
+
Când se termină, avem un rezultat`pow(2, 3) = 8`.
263
263
264
-
The recursion depth inthis case was:**3**.
264
+
Adâncimea de recursivitate în acest caz a fost:**3**.
265
265
266
-
As we can see from the illustrations above, recursion depth equals the maximal number of context in the stack.
266
+
După cum putem vedea din ilustrațiile de mai sus, adâncimea de recursivitate este egală cu numărul maxim de contexte din stack.
267
267
268
-
Note the memory requirements. Contexts take memory. In our case, raising to the power of`n`actually requires the memory for`n`contexts, for all lower values of`n`.
268
+
Observați cerințele de memorie. Contextele ocupă memorie. În cazul nostru, ridicarea la puterea lui `n`necesită de fapt memorie pentru`n`contexte, pentru toate valorile mai mici ale lui`n`.
269
269
270
-
Aloop-based algorithm is more memory-saving:
270
+
Un algoritm bazat pe loop este mai economic pentru memorie:
271
271
272
272
```js
273
273
function pow(x, n) {
@@ -281,13 +281,13 @@ function pow(x, n) {
281
281
}
282
282
```
283
283
284
-
The iterative `pow`uses a single context changing `i`and`result`in the process. Its memory requirements are small, fixed and do not depend on`n`.
284
+
Iterativul `pow`utilizează un singur context care schimbă `i`și`result`în timpul procesului. Cerințele sale de memorie sunt mici, fixe și nu depind de`n`.
285
285
286
-
**Any recursion can be rewritten as aloop. The loop variant usually can be made more effective.**
286
+
**Orice recursivitate poate fi rescrisă ca unloop. De obicei, varianta loop poate fi făcută mai eficientă.**
287
287
288
-
...But sometimes the rewrite is non-trivial, especially when a function uses different recursive subcalls depending on conditions and merges their results or when the branching is more intricate. And the optimization may be unneeded and totally not worth the efforts.
288
+
...Dar uneori rescrierea nu este trivială, în special atunci când o funcție utilizează diferite subapelări recursive în funcție de condiții și fuzionează rezultatele acestora sau când ramificarea este mai complexă. Iar optimizarea poate fi inutilă și nu merită în totalitate eforturile depuse.
289
289
290
-
Recursion can give a shorter code, easier to understand and support. Optimizations are not required in every place, mostly we need a good code, that's why it's used.
290
+
Recursivitatea poate oferi un cod mai scurt, ușor de înțeles și de susținut. Optimizările nu sunt necesare în orice loc, de cele mai multe ori avem nevoie de un cod bun, de aceea este folosit.
0 commit comments