Identifying CSV length in D3.js before loading - javascript

So far every resource I have seen on loading CSV files into D3.js require that the only way to use the data is to define callback functions (due to Javascript's asynchronous execution).
I originally approached this problem the usual way of defining functions outside then calling them inside the callback function.
However this proved repetitive. The other alternative however, to define everything in the callback function, would lead to repeated executions of the same thing leading to redundancy.
I personally found that if I knew how many data items were going to be used, I could simply set an
if (read_data_length==total_data_length)
condition so that all my code is executed once every CSV row is read.
My question is whether there is a way to identify the length of a CSV file (in terms of number of Objects, or rows), BEFORE loading a CSV file in order to establish such a limit?
So far my own insight has proven this is difficult to impossible, but I am hoping somebody with more experience in Javascript might be able to clear this up.
Thanks in advance.

Related

Count the javascript functions on a page (with javascript)

Good day all.
I would like to count the js functions present on a given page, and then send this number via ajax (the ajax part is the simple part) do you think is it possible to achieve that in javascript? what should be the best way to do it?
thanks in advance.
explanation:
I'm trying to figure out how to counter measure some fraud attempts on some subscription pages, I suspect that some javascript is injected on the page before the user click, so having the number of functions present at the load event, and then the number of those present on the submit event, should lead me in the right direction.
Well, if someone is injecting code to your site, they could just as easily use that code to turn off your code counting functions. You can never trust anything that happens on the client side and must validate everything on the server.
As for the technical side, you'd use a tool like acorn to traverse the syntax tree and find all FunctionDeclaration and FunctionExpressions (and arrows, concise method definitions and methods). That would not find all functions, but it would find all statically created ones.
Once the code started executing it's impossible since it's easily reducable to the halting problem. You don't know if a code will create a function at some point in the future.

How to resolve coordinates of selected objects

I need to get coordinates of selected objects in indesign project. It would be probably some javascript code to do this.
Goal is, to have all objects with their coordinates exported to xml or any other format I can work further with.
An idml export would provide all the info you need. However it would require a good understanding of IDML syntax.
A snippet export woukld provide good informations too, easier to read than the whole IDML stuff but yet limited per object.
A script can also output the informations you need but it am not aware of an existing one. Fact is that inspite the appearances, getting the "corrdinates" may need serious computations are items could be rotated a/o skewed thus affecting the global bounds.
You should investigate Adobe Scripting Forum. I guess this kind of needs have already been exposed there.

Does the result of a function run several times is cached?

In JavaScript, I've an object with an array, and a method wich gets a slice of that array and a concatenation with another array.
If that method is run several times in the same function to return always the same value, does the performance will be faster after of the first run (due to the result will be cached in CPU cache)?
Of course no is the only answer would be here. Because the purpose of a function is to take some parameters and return a value. All the parameters might be different each time you call the function and even if they are the same, the result might be different, also event if you call the function and each time it returns the same result, because it might do an action or cause some modifications in other places, caching the result by the parser would be a buggy idea.
Cheers
Maybe.
There are quite a lot of levels of cache to look at, here. Your processor alone has more than a single cache. Basically, though, you simply can't say much about those. They might have different sizes, things like what other things you do in the mean time and how long the function is all influence this. It should also be noted that this does not work at the level of what you call a function call when in Javascript, but at a much lower level. However, it might at times mean that some time can be shaved off of the execution time of the function. I don't think it's too likely or noticeable, but in the end, you can't really say much about it.
Finally, there is javascript itself. Per the standard, it doesn't have such caching. However, the standard doesn't prohibit strange caching either, so there might one day be a browser that does it like that (I don't believe there is one right now.)
In the end, the basic answer is: no not in a noticeable way. However, there might actually be a speed gain due to the cache, it's always hard to say.
I guess the general answer to this question is NO!
There is no caching in JavaScript or CPU caching that you can control with JavaScript. If you need to cache something / increase performance, i will have to program that yourself.
See this small example:
http://jsperf.com/cachingjq
No, you'd have to manually (or with a framework) memoize the results: Javascript Memoization Explanation?

Is there a way to remove unused lines of a JS Library?

I currently coded something awesome based on a JS Library, and I want to remove every useless line that is doing nothing but take more time to download from the server.
What is the best way to keep track of what piece of code was used/unused when my code executed? (So I can remove the unused code)
My first thoughts are to put "var log += "<br />Function name was used." on every function and remove every function that wasn't called. But I am curious about if there is another way
I want to point out that modifying certain JS Libraries might violate their Licences and possibly cause strong legal issues. So if anyone is reading this and is planning to do the same thing as me, please read carefully the Licence(s) before you even attempt to do this!
In my estimation, the best way to keep track of which code has actually executed would be to use a code coverage measurement tool. There are several available for Javascript, many of which are outlined in a previous question: https://stackoverflow.com/questions/53249/are-there-any-good-javascript-code-coverage-tools .
Of course, this only tracks the code that has executed as a result of the test suite you are running it against, and would not be a foolproof way to find "completely dead" (i.e. unreachable) code. But it's a start...

how do you folks handle complex state situations where order of operations is important?

I'm getting in to a situation where I have several interacting widgets (on a web UI), all of whom can be in multiple different states, and whose behavior depends on others the others. I'm running in to situations where, for example, a set of data gets sorted twice, or the data gets displayed before it's sorted, rather than the other way around. It's a little bit of a wack-a-mole problem, where I think I've simplified things and gotten it working, only to find out I've broken things somewhere else.
I have functions that do things like:
widgetAFunction
load data into widget B
tell widget B to sort the data
tell widget B to display the data
My love of code reuse makes me want to do something like write a loadData function in widget A that goes something like this:
widgetBLoadDataFunction
update data
sort the data
refresh the view
So that all widgetA has to do is call one function on widgetB. But then there are cases where I just want to sort the data, without updating the data, so I write:
widgetBSortFunction
sort the data
refresh the view
And then maybe I want a filter function
widgetBFilterFunction
filter the data
refresh the view
And maybe I want to be update the data but not sort it, so I have
widgetBNoSortLoadDataFunction
update data
refresh the view
It doesn't seem that complex, but I wind up with these really long, very brittle chains of function calls, or a bunch of very similar calls. As Martin Fowler would say, the code is getting a little smelly.
So, what other alternatives do I have? I did something on a recent project where I did a state machine kind of thing, where I registered a bunch of functions with a set of conditions, or states which would trigger their execution. That worked somewhat well, and I'm thinking that approach might be good to use again.
Does anyone know what I'm talking about here, and even better, can anyone point me toward some patterns that will help me get my head around this better?
What you need is a finite state machine implementation. Basically every finite state machine needs:
Events that the program responds to
States where the program waits between events
Transitions between states in response to events
Actions taken during transitions
Variables that hold values needed by actions between events
A good article from IBM teachs you a way of implementing it by means of Javascript.
Edit: Here is a FSM builder, so you don't have to build your own.
Fernando already mentioned FSMs, and gave good info and links. :)
In addition, I'll add that your classes should already incorporate enough state so that you're not worried about sorting twice, etc. I.e., widgetB.sort() should check if it's been sorted since last update and just return if so. There's practically no downside to doing this, and it can improve performance (and also guard consistency).

Categories