Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #32789 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2017-03-17 02:00 -0700 |
| Last post | 2017-03-18 15:01 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.javascript
Save instance of array "undo" and copy it back to array. jonas.thornvall@gmail.com - 2017-03-17 02:00 -0700
Re: Save instance of array "undo" and copy it back to array. JJ <jj4public@vfemail.net> - 2017-03-18 18:53 +0700
Re: Save instance of array "undo" and copy it back to array. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-03-18 15:01 +0100
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2017-03-17 02:00 -0700 |
| Subject | Save instance of array "undo" and copy it back to array. |
| Message-ID | <358e280c-4521-42fe-abcb-6926efa129c4@googlegroups.com> |
Is there a way to get javascript tell how much space an array take up in memory?
If not, how would one go on calculate it?
I was thinking saving one or a few old instances of an array, creating an undo functionality. Could someone show the principle for that using a plain integer array.
This is my array and its attributes.
function autoNodes() {
for (var m = 0; m < nodes; m++) {
arr[m] = {
nodelinks: [],
hold: false,
rposX: 0,
rposY: 0,
rwidth: 10,
rheigth: 10,
rcolor: "red",
bcolor: "green",
print: true
};
}
}
[toc] | [next] | [standalone]
| From | JJ <jj4public@vfemail.net> |
|---|---|
| Date | 2017-03-18 18:53 +0700 |
| Message-ID | <5y5t1y7xyqf0$.1g6m6w8017g2$.dlg@40tude.net> |
| In reply to | #32789 |
On Fri, 17 Mar 2017 02:00:39 -0700 (PDT), jonas.thornvall@gmail.com wrote:
> Is there a way to get javascript tell how much space an array take up in memory?
No.
> If not, how would one go on calculate it?
It depends on what type of values are stored in each array element.
- Boolean type only need one byte but is rounded up to either 32-bit or
64-bit integer [1].
- Number type is a 64-bit floating point number.
- String type is either a 32-bit or 64-bit integer [1] which is a memory
pointer pointing to the actual UTF-8 string data. i.e. it has two data:
pointer & text.
- Date type is either a 32-bit or 64-bit integer [1] which holds the
timestamp in either time_t or time_t64 format. This can also be a 64-bit
floating point number, depending on the JavaScript engine.
- Object and Array types are either a 32-bit or 64-bit integer [1] which is
a memory pointer pointing to a structure array. Each structure array element
is a memory pointer to the actual structure data that holds object member's
key name, value name, and attributes (e.g. writable, enumrable, value type,
value data, etc.).
- Function type is either a 32-bit or 64-bit integer [1] which is a memory
pointer pointing to a function reference. Depending on the JavaScript
engine, a function reference can be an index to an array of function data
that holds e.g. function name, number of arguments, scope, etc. Since
functions can also have members, a function data also holds object-like
data.
If you need to calculate the exact memory consumption, you'll have to refer
to the JavaScript engine's source code. Each JavaScript engine consumes
different amount of memory since they have different design.
[1] Depending whether the JavaScript engine is compiled as 32-bit or 64-bit
code.
> I was thinking saving one or a few old instances of an array, creating an
> undo functionality. Could someone show the principle for that using a
> plain integer array.
>
> This is my array and its attributes.
>
> function autoNodes() {
> for (var m = 0; m < nodes; m++) {
> arr[m] = {
> nodelinks: [],
> hold: false,
> rposX: 0,
> rposY: 0,
> rwidth: 10,
> rheigth: 10,
> rcolor: "red",
> bcolor: "green",
> print: true
> };
> }
> }
An undo feature track each data changes by recording each operation used to
apply the changes. The record may include the state of the data before the
changes, if necessary.
In your example code, the operation would be the assignment of the array
element. Since undoing a variable assignment requires you to know the
original variable value, you'll need to store the original value before the
variable assignment is actually performed. I'd recommend using the
`Object.watch()` to do this.
IOTW, what things to record is the information required to restore something
into its previous state.
Other operation e.g.
Operation: Insert array element.
Record: Element insertion index.
Undo: Delete the element pointed by element index in the record.
If you need to implement a redo feature, you'll have to store the value used
to assign to the variable.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-03-18 15:01 +0100 |
| Message-ID | <1968884.iZASKD2KPV@PointedEars.de> |
| In reply to | #32793 |
JJ wrote: > On Fri, 17 Mar 2017 02:00:39 -0700 (PDT), jonas.thornvall@gmail.com wrote: >> If not, how would one go on calculate it? > > It depends on what type of values are stored in each array element. Most certainly not. The values referred by array elements are most certainly _not_ part of the memory required for storing the array. An efficient implementation would store pointers in the array only. > - Boolean type only need one byte but is rounded up to either 32-bit or > 64-bit integer [1]. No, it depends on the implementation of ECMAScript. > - Number type is a 64-bit floating point number. At minimum, to satisfy IEEE 754 requirements. > - String type is either a 32-bit or 64-bit integer [1] which is a memory > pointer pointing to the actual UTF-8 string data. i.e. it has two data: > pointer & text. Again, that depends on the implementation. > - Date type is […] > - Object and Array types are […] > - Function type is […] See above. > […] a structure array […] Another figment of your vivid imagination. > depending on the JavaScript engine. Not “JavaScript engine”, but _implementation of ECMAScript_. The various languages whose name contains “JavaScript” are _not_ the only implementations of ECMAScript. You have been told this before ad nauseam. Will you *ever* learn? > In your example code, the operation would be the assignment of the array > element. Which is _not_ a “variable assignment”, but a property-write access. > Since undoing a variable assignment requires you to know the > original variable value, you'll need to store the original value before > the variable assignment is actually performed. I'd recommend using the > `Object.watch()` to do this. Even your half-knowledge is out of date. Object.*prototype*.watch() is proprietary (implementation-dependent, precisely SpiderMonkey-only), intended for *debugging*, and is deprecated (in favor of getters/setters and/or Proxy, I presume). Object.observe(), which you might have meant instead, is proprietary, and deprecated in favor of getters/setters and/or Proxy, too. <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch> pp. -- PointedEars FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web