.split() on a very lengthy string gives LOW MEMORY error #1451
Replies: 13 comments
-
Posted at 2021-02-14 by @fanoush read() is OK, split() is not. You can search for end of line one by one (via String.indexOf) instead of making big array of strings. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by @gfwilliams As @fanoush says you can use indexOf - there's actually an example of this in the planetarium app: https://github.com/espruino/BangleApps/blob/master/apps/planetarium/planetarium.app.js#L107
Just a bit of background: I guess there's no JavaScript function that is the equivalent of |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by NewAtEspruino Thanks a lot, @fanoush and @gfwilliams for your replies. Until I received the responses here, I did try reading the file using indexOf. Obviously, since 17K is a very large number, I for instance tried reading only the first 500 values. (Could separate out every value from the file based on carriage return ) and could print all of them. No problem. However, the moment I try to store these 500 values in an array, I face the low memory problem. Thanks a lot once again for all your help! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by @gfwilliams Are you storing numbers? This might help you: http://www.espruino.com/Performance#array-buffers-are-the-most-efficient-way-to-store-data Basically normal arrays are flexible but pretty inefficient. All you need to do is use a typed array to store substantially more data - for example if all your data could fit into a single byte, you could fit over 30,000 items in memory. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by @fanoush or you can write it back to storage as binary (or initally save it already like that instead of text csv) and then you could read it directly into array via https://www.espruino.com/Reference#l_Storage_readArrayBuffer Then you could iterate over all of them like you can with the big string now. see the Description there. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by NewAtEspruino Hi @gfwilliams, |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by NewAtEspruino Hi @fanoush, |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by NewAtEspruino Hello @gfwilliams, var arr_data = new Float32Array(300); since my numbers are in decimals. I tried using Float64Array and it gave a better result however not the exact value I believe using Float32Array would be better considering memory usage, however not sure why the values aren't getting stored in the original form Thanks! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-15 by @fanoush From https://en.wikipedia.org/wiki/Single-precision_floating-point_format "This gives from 6 to 9 significant decimal digits precision" you have 10 digits so won't fit into float32, as for float64 10 digits should fit but still not every value can be represented exactly, see e.g. https://stackoverflow.com/questions/12165216/which-values-cannot-be-represented-correctly-by-a-double |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-16 by @gfwilliams Float64Array uses exactly the same number format as JavaScript uses internally, so you'll find that there is no loss between just Also, the value may actually be pretty much ok, but Espruino is just not converting it to a string in exactly the same way as you wrote it. For example 1180.952381 and 1180.95238100000 are basically the same number, just with some extra Another option you have is if you always have 6 decimal places, just store the value as an integer in a Int32Array ( |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-16 by @gfwilliams Forgot to add - @fanoush's is a great idea as well. Especially if you write in binary to begin with, you can end up with very fast array access but without using any RAM. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-16 by NewAtEspruino Thank you so much @fanoush and @gfwilliams for the explanation. Thanks for all your help as always! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-02-18 by Robin Wed 2021.02.17
@NewAtEspruino while I don't have a solution to your initial inquiry, I am able to provide a link to a wealth of other links that should assist in your curosity discovery. The simple answer is found in the differences and complexities that result when using both I noticed in post #1 that text chars Had to revisit this topic over a year ago and included many links and examples to how floating point works under the hood. Note in post #4 (there) the number of digit differences between browsers and Espruino |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2021-02-13 by NewAtEspruino
Hello,
I am trying to read a very lengthy(approx. 15K rows) .csv file using require("Storage").read("xxx.csv").
And the next step is to create an array for the values in the .csv file.
However, .split("\r\n") function on require("Storage").read("xxx.csv") gives me an error - see the attached image for the error details.
Is there a way that I could read only say first 200 rows from the .csv at a time? and then the next 200?
I don't see any other API at the moment to read the file in chunks, except .read(), so wanted to confirm.
Pls. note that I am using Espruino emulator and also have some other functionality coded at the moment for the smart watch.
Thanks a lot!
Attachments:
Beta Was this translation helpful? Give feedback.
All reactions