Code Monkey home page Code Monkey logo

Comments (6)

hbi99 avatar hbi99 commented on May 19, 2024

Hi @charbel14,
Thanks for using DefiantJS...I've been thinking of improving the performance with two different approaches and I think I've decided the way to go. Roughly speaking, Defiant prepares a JSON structure for search and then performs the "actual search". The preparation is what takes the longest time and does not need to be re-prepared, especially if there is no change to the JSON data.

The addition I will add, will look something like the code below and I think it's the best approach. It is "backwards" compatible. That is, it doesn't affect the way DefiantJS has been used so far and the new version of the lib can be replaced with the old, without any risk for breaking anything.

// JSON data
var data = {... json-data...};

// create a re-usable snapshot
var snapshot = Defiant.getSnapshot(data);

JSON.search(snapshot, '//leaf');  // this search is much faster
JSON.search(data, '//leaf');      // ...than this one

//...especially if the snapshot is re-used several times
JSON.search(snapshot, '//leaf');  

It would be great to get feedback on this...what do you think?
Notice that this pattern gives the implementing developer the responsibility to use updated snapshots of the JSON structure.

As for your second question; there is no difference if you have full path expression. Your JSON data might contain a structure that matches your expression multiple times - hence the search goes through the full structure regardless.

Therefore, IMHO, the best expression if what makes it easiest to read, when you re-visit the code you wrote today, in the future. The best code is the one that supplies the fastest overview.

from defiant.js.

charbel14 avatar charbel14 commented on May 19, 2024

Yes that makes sense. i.e. to compile before hand.. and I did expect that this is what is slowing it down.

By the way the search(data, path,true ) did not seem to work for me. I had an array of 800 or so entries and I tested something right at the index 800 and something at index 1, I expected that with true the 1 should be faster and 800 would be slower.

But did not get any result back for either

Thanks again.

Charbel

from defiant.js.

hbi99 avatar hbi99 commented on May 19, 2024

@charbel14,
I have applied the snapshot feature and my tests shows an improvement of more than 100 times. Check out the "json-search-4.htm" in the demo folder, on this branch:
https://github.com/hbi99/defiant.js/tree/snapshot-feature

I tested searching on a ~1.6MB large JSON file (also in demo folder) on Firefox.

Search without snapshot took: 561 ms
Search with snapshot took: 4 ms

I think this is an acceptable improvment :)

PS: I also fixed the JSON.search(data, path, true) feature...that is, returning the first occurrence of the query. Though, with the snapshot improvement, the difference might be 3ms now on a big JSON file. On smaller files, it might be no difference at all (at best).

from defiant.js.

charbel14 avatar charbel14 commented on May 19, 2024

I can confirm the above figures. I ran my test data and it works.

The first occurrence feature does not seem to be working .. but as you mentioned the timing difference makes it redundant almost.

Thanks for the quick turnaround.

When will this be pushed to main.?

from defiant.js.

hbi99 avatar hbi99 commented on May 19, 2024

Now :)

Thanks for testing it on your end as well

from defiant.js.

hbi99 avatar hbi99 commented on May 19, 2024

I had forgotten to grunt to project before merging it...sorry.
It's now done and updated

from defiant.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.