简体   繁体   中英

creating a calendar in javascript

I am in the middle of creating a calendar in javascript but am contemplating using the date functions to retrieve values or splitting the date string to retrieve values. I tried looking up the big o for these functions but have not found any. I want to know what is fastest.

For example

var date = new Date();
date.toString().split(" ")[1]; //this will get month name

vs

var date = new Date();
date.getMonth() //this will get month

they might not be the same but my question is which one is faster. edit i forgot toString.

As the split() method use searching by pattern algorithm and returns an Array which is f(n) = n = O(n) [Big O notation] it is good performance wise.

But Date.prototype.getMonth() involve no searching and return the Number of the month – 1 as it starts counting at 0. (Source: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date/getMonth ) f(n) = n = O(1).

Conclusion: Date.getMonth is faster in time and space complexity then.split() method

I would use the getMount() method, it's a built in the object so it's more reasonable to me to use it along with all the other methods. When you say "faster" you mean write less code or the code execution is faster?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM