यह वास्तव में वी 8 द्वारा कुछ अतिरिक्त चाल है।
एक JSObject
(आंतरिक सी ++ एक जे एस Object
का प्रतिनिधित्व), जबकि "properties" जे एस स्ट्रिंग सूचकांक के साथ जिम्मेदार बताते हैं, जिसमें दो गुणधर्म हैं elements
और properties
, जहां "तत्वों" जे एस संख्यात्मक सूचकांक के साथ जिम्मेदार बताते हैं।
जाहिर है, संख्यात्मक सूचकांक यहां बहुत कम स्मृति का उपभोग करते हैं, क्योंकि संपत्ति के नामों को संग्रहीत करने की आवश्यकता नहीं है। सांख्यिक तत्वों के भंडारण के लिए नामित किया गया गुण के भंडारण के लिए एक, एक और:
http://code.google.com/intl/de-DE/chrome/devtools/docs/memory-analysis-101.html#primitive_objects
एक ठेठ जावास्क्रिप्ट वस्तु दो सरणियों posesses।
यह v8 स्रोत कोड से देखा जा सकता है:
http://code.google.com/p/v8/source/browse/trunk/src/objects.h#1483
// [properties]: Backing storage for properties.
...
// [elements]: The elements (properties with names that are integers).
http://code.google.com/p/v8/source/browse/trunk/src/runtime.cc#4462
MaybeObject* Runtime::SetObjectProperty(Isolate* isolate,
Handle<Object> object,
Handle<Object> key,
Handle<Object> value,
PropertyAttributes attr,
StrictModeFlag strict_mode) {
...
// Check if the given key is an array index.
uint32_t index;
if (key->ToArrayIndex(&index)) {
// In Firefox/SpiderMonkey, Safari and Opera you can access the characters
// of a string using [] notation. We need to support this too in
// JavaScript.
// In the case of a String object we just need to redirect the assignment to
// the underlying string if the index is in range. Since the underlying
// string does nothing with the assignment then we can ignore such
// assignments.
if (js_object->IsStringObjectWithCharacterAt(index)) {
return *value;
}
Handle<Object> result = JSObject::SetElement(
js_object, index, value, attr, strict_mode, set_mode);
if (result.is_null()) return Failure::Exception();
return *value;
}
if (key->IsString()) {
Handle<Object> result;
if (Handle<String>::cast(key)->AsArrayIndex(&index)) {
result = JSObject::SetElement(
js_object, index, value, attr, strict_mode, set_mode);
} else {
Handle<String> key_string = Handle<String>::cast(key);
key_string->TryFlatten();
result = JSReceiver::SetProperty(
js_object, key_string, value, attr, strict_mode);
}
if (result.is_null()) return Failure::Exception();
return *value;
}
// Call-back into JavaScript to convert the key to a string.
bool has_pending_exception = false;
Handle<Object> converted = Execution::ToString(key, &has_pending_exception);
if (has_pending_exception) return Failure::Exception();
Handle<String> name = Handle<String>::cast(converted);
if (name->AsArrayIndex(&index)) {
return js_object->SetElement(
index, *value, attr, strict_mode, true, set_mode);
} else {
return js_object->SetProperty(*name, *value, attr, strict_mode);
}
}
मैं विस्तार में नहीं जाना होगा, लेकिन ध्यान दें कि SetObjectProperty
कॉल या तो कुंजी के आधार पर SetElement
या SetProperty
। सुनिश्चित नहीं है कि चेक आपके परीक्षण मामले key = i + '0'
में क्यों विफल रहता है।