Coder Social home page Coder Social logo

[Question] Why is iterating over range.areas.getItemAt(x) so much faster than calling range.areas.load('items')? about office-js HOT 4 CLOSED

hdwatts avatar hdwatts commented on August 10, 2024
[Question] Why is iterating over range.areas.getItemAt(x) so much faster than calling range.areas.load('items')?

from office-js.

Comments (4)

XuanZhouMSFT avatar XuanZhouMSFT commented on August 10, 2024

@hdwatts Thanks for the waiting. I did a quick investigation and found it should caused by the "areas.load('items')" you are using in the first code. With this line, we will try to load all properties for Range/RangeAreas. It will cost a lot of time/resource to do that.
For example, with the below code we can see the result is:

areas.load("items");
console.log("before: " + JSON.stringify(areas));
await context.sync();
console.log("after: " + JSON.stringify(areas));

image

So we'd like to suggest you to only load the properties you need.

from office-js.

hdwatts avatar hdwatts commented on August 10, 2024

@XuanZhouMSFT

In my first code example, I'm doing what you do (loading all the items). I agree, this is loading a lot of data.

However, in my second code example, I'm loading what I believe to be the same data (loading all the items). I'm just iterating over the number of items and using the getItemAt() method.

The latter example runs in drastically less time, but accomplishes the same thing. Is there a difference between these two methods?

from office-js.

XuanZhouMSFT avatar XuanZhouMSFT commented on August 10, 2024

@hdwatts They are different. The RangeArea.getItemAt() method will only return an object for rangeCollection. Within this object, the properties are not loaded. If you want to use the property inside it, you will also need to load it/them, wait for the the context.sync and then get. And if you directly get the property, it will throw exception.

from office-js.

hdwatts avatar hdwatts commented on August 10, 2024

Aha! Thank you so much @XuanZhouMSFT! That answers it for me and satisfies my curiosity :). Thanks again!

from office-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.