0

I am writing a server-side application using JavaScript (Express). So I have to validate one big stream of chars (base 64 string). I kinda know what I want to do and how but I have performance related question.

Consider that the string that is uploaded is really big (up to 5 MB). I have already written couple of functions that should do the validation but I am not aware of what is going on behind the hood.

function validate(str) { .... return bool; } var b64_string = '......'; // <- string can be 5 megabytes if(validate(b64_string) { doSomething(b64_string); } 

If this was C a stack would be allocated for validate(str) function and there would be 5mb of memory for passed variable.

But what happens in JavaScript engine? Is there any way of sending 'pointer' to function so memory consumption does not get too big? T

Thanks in advance!

1
  • I wonder why the down vote with no explanation ¯_(ツ)_/¯ Commented Apr 7, 2016 at 5:56

1 Answer 1

2

objects in javascript are passed by reference.

I believe integers and strings (etc) are not, so be careful of that point.

Consider adding your string to an object hash reference which you can then pass by reference down the chain.


for example:

var hashRef = {}; hashRef.b64_string = '......'; // <- string can be 5 megabytes function validate(hashRef) { .... return bool; } if(validate(hashRef)) { doSomething(hashRef); } 
Sign up to request clarification or add additional context in comments.

6 Comments

nice. I'll check it out. Since my functions both validate and modify i considered using closures - but i have separated my validation to a module so it is a pain... Thanks anyway I'll try (and will not forget to +1 the answer of yours)
No problem @Rouz happy to help ^_^
" I believe integers and strings (etc) are not, so be careful of that point". Strings are passed by reference internally in most engines.
@Ginden in what js engines are strings pass by reference?
V8 passes strings by reference internally.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.