Question on Best Practice for Data Binding / Performance of ToArray()/new List<T>
See original GitHub issueFirst off, this is awesome. As someone coming from C# picking up TypeScript, this makes me feel a lot more at home.
Question regarding usage of these magic Lists. In a js framework, e.g. Vue, obviously they would expect Arrays for binding (i.e. can’t bind directly to List. So is it better to 1) keep your data as a List in the component and just call ToArray on it for binding, or 2) keep your component data as an array that you bind to, have a computed prop that turns it into a List, and use that computed prop for manipulation.
Which would be the recommended approach in general, or is there a better way I’m missing? Which in most cases would be more performant? Would appreciate any insight.
Here’s a Vue example of option 2:
@Component
export default class App extends Vue {
servers: IServer[] = [
{ name: "Ol Faithful", num: 1, age: 10, status: "Up" },
{ name: "Rackspacer", num: 2, age: 3.5, status: "Up" },
{ name: "Basement Dweller", num: 3, age: 5, status: "Down" },
{ name: "Big Rig", num: 4, age: 7, status: "Unknown" },
{ name: "Dyno-mite", num: 5, age: 1, status: "Up" }
];
get serverList() {
return new List(this.servers);
}
testLINQ() {
// test updating data
this.serverList.First().name =
this.serverList.First().name + " - updated via LINQ";
// test linq functions
let maxAge = this.serverList.Max(s => s.age);
let oldestServer = this.serverList.FirstOrDefault(s => s.age === maxAge);
alert(`Oldest server is ${oldestServer.name} with an age of ${maxAge}.`);
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
C#: ToArray performance [duplicate] - Stack Overflow
Regardless, it's generally a good practice to avoid calling .ToArray() and .ToList() unless you absolute require it.
Read more >Top 50 Java Collections Interview Questions and Answers in ...
This article will share a complete list of top Java Collections interview questions which are most likely to be asked in Java Interviews...
Read more >Collection.toArray(new T[0]) or .toArray(new T[size]) - Baeldung
The toArray() method allocates a new in-memory array with a length equal to the size of the collection. Internally, it invokes the Arrays....
Read more >VBA ArrayList - A Complete Guide - Excel Macro Mastery
Forget about the VBA Collection. The VBA ArrayList is far more powerful and it's simple to use. This post covers everything you need...
Read more >Optimize ToArray and ToList by providing the number of ...
In this scenario, ToArray is fairly efficient. Now, let's change that code to extract just the names from the users: List<User> users =...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FIY, I just ended up passing in an observable array to the List constructor to get it to respons to Mobx’s change notifications (you can use them in Angular templates now and it will automatically update itself whenever an item is added to or removed from the list):
let list = new List<SomeObject>(observable.array());
Thought I would add this for future users of this great library and mobx. Linq.ts is not compatible with mobx’s change detection, so to get it to work, you would always use a computed property for the Linq list, and use an array for the observable property like so:
Stackblitz example: https://stackblitz.com/edit/mobx-linqts
My fantasy is to have linq.ts modified to be compatible with Mobx’s change detection (which uses Javascript proxy objects to implement its change detection). Then I’d have the dream combination 😃