2

jQuery documentation says like jQuery.data() is low-level method and suggests to use (not said but think of high-level methods) like .data() .

So, my question is:

  • Why does it say a more convenient way is like .data() ?
  • Does low-level methods have slower access?
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 1
    low lever methods could be faster because that removes some generalization code which normally wraps a high level method – Arun P Johny Jul 02 '14 at 06:04
  • By low-level method, I think it meant the methods which explicitly interacts with DOM. – kshirish Jul 02 '14 at 06:05
  • [This might help to clear the difference between `high-level and low-level methods`](http://stackoverflow.com/a/633623/2260614) – Bhavik Jul 02 '14 at 06:08

4 Answers4

1

The 'high level' and 'low level' methods are classified based on the level of abstraction it has.

For example,

$('#test').hide();

This is an example of an high level abstraction where you just need to call hide() to hide the element.

On the flip side,

$('#test').css({'display':'none'});

This is jQuery as well but more details are being explained. Hence it is low level and .css() enables us to give more options to it as well.

In your particular example when it says .data() is a more convenient way, it means that it is low level and has more flexibility over how it can be used.

vasa
  • 787
  • 8
  • 21
1

In Programming Language Low-level does not mean bad or slow, the meaning is based on their access and abstraction level. Low-level are typically lower in the abstract hierarchy. For example C is a low level language because it can give you direct assembly level coding but it does not give you an interface. C++ is a mid level language because it gives you both low-level access like C and high level abstraction layer with a windows UI. Java is a high-level language it only gives you top level coding environment and the JVM handles others like memory management, etc.

Similarly, low-level jQuery.data, it gives you flat access to all attributes stored in data namespace, but also gives you access to all data saved by jQuery itself and other plugins. But $(....).data only gives you high level access, means it abstracts all other access and data and namespaces and gives you only what you need, the data for the selector only (mentioned in $(..)).

So, in general you are to do this -

  1. Avoid using jQuery.data or $.data
  2. Use jQuery(....).data or $(....).data
brainless coder
  • 6,310
  • 1
  • 20
  • 36
1

Does low-level methods have slower access?

NO , Check this JS Perf test.

Test .data()

jQuery.data($el, 'key', 'plop');    //faster

$el.data('key', 'plop');            //Approx 30% slower.

Why does it say a more convenient way is like .data() ?

  • .data() : Can access data attributes :

    <div id="content" data-key="hey"></div>
    $('#content').data('key', 'plop');
    
  • While jQuery.Data() cannot.


To conclude :

  • If you deal with an HTML5 data-* attribute or event, use $.fn.data

  • In other cases, use $.data/jQuery.data() because of performance

Note: This is just an example considering .data()

Shaunak D
  • 20,588
  • 10
  • 46
  • 79